收到提取POST请求的响应后,如何将用户重定向到页面? [英] How to redirect user to a page after receiving the response from a fetch POST request?
问题描述
我正在为使用javascript的fetch()API向node.js服务器发送POST请求的Web应用程序编写代码.在成功的请求服务器上,通过重定向进行响应,此重定向url在fetch()api响应主体中接收.之后,我应该怎么做才能将用户从提取功能重定向到该URL.
I am writing code for a web application that send a POST request to node.js server using fetch() api of javascript. On successful request server responds with a redirection, this redirection url is received in the fetch() api response body. After this what should I do to redirect user to this URL from fetch function.
fetch("/events/registration", {
method: "POST",
redirect:"follow",
headers: {
"Accept": "application/json , text/plain ,*/*",
"Content-type": "application/json"
},
body: JSON.stringify({
name,
email,
})
})
.then((res)=>res.json())
.then((data)=>{console.log(data);if(data.err)alert(data.err);Response.redirect()});
作为响应,我在获取api中收到redirect:true , url: "LinkOfRedirection/redirect"
,现在该怎么做才能将用户从此处重定向到LinkOfRedirection/redirect.
In response that i receive in fetch api I am getting redirect:true , url: "LinkOfRedirection/redirect"
, now what should i do to redirect user from here to the LinkOfRedirection/redirect.
推荐答案
// Sets the new location of the current window.
window.location = res.url;
// Sets the new href (URL) for the current window.
window.location.href = res.url;
// Assigns a new URL to the current window.
window.location.assign(res.url);
// Replaces the location of the current window with the new one.
window.location.replace(res.url);
// Sets the location of the current window itself.
self.location = res.url;
// Sets the location of the topmost window of the current window.
top.location = res.url;
或者您可以使用
res.redirect(301, res.url);
这篇关于收到提取POST请求的响应后,如何将用户重定向到页面?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!