我正在为使用 javascript 的 fetch() api 向 node.js 服务器发送 POST 请求的 Web 应用程序编写代码。在成功的请求服务器响应重定向,这个重定向 url 被接收到 fetch() api 响应主体。在此之后,我应该如何将用户从获取功能重定向到此 URL。
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()});
作为对我在 fetch api 中收到的响应,我得到了 redirect:true ,url: "LinkOfRedirection/redirect"
,现在我应该怎么做才能将用户从这里重定向到 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);
关于javascript - 如何在收到获取 POST 请求的响应后将用户重定向到页面?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54414143/