我正在为一个 Web 应用程序编写代码,该应用程序使用 javascript 的 fetch() api 向 node.js 服务器发送 POST 请求。成功请求后,服务器会进行重定向响应,该重定向 URL 将在 fetch() api 响应正文中接收。之后我应该做什么来将用户从 fetch 函数重定向到此 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);
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)