Javascript 如何在收到fetchpost请求的响应后将用户重定向到页面?
我正在为一个web应用程序编写代码,该应用程序使用javascript的fetch()api向node.js服务器发送POST请求。在请求服务器成功响应重定向时,将在fetch()api响应正文中接收此重定向url。在此之后,我应该如何将用户从fetch函数重定向到此URLJavascript 如何在收到fetchpost请求的响应后将用户重定向到页面?,javascript,node.js,redirect,fetch,Javascript,Node.js,Redirect,Fetch,我正在为一个web应用程序编写代码,该应用程序使用javascript的fetch()api向node.js服务器发送POST请求。在请求服务器成功响应重定向时,将在fetch()api响应正文中接收此重定向url。在此之后,我应该如何将用户从fetch函数重定向到此URL fetch("/events/registration", { method: "POST", redirect:"follow", headers: {
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()});
作为我在FetchAPI中收到的响应,我得到了重定向:true,url:“linkofreirection/redirect”
,现在我应该如何将用户从这里重定向到linkofreirection/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);
解决这一问题的简单方法如下: 获取(“/api/login/”{ 方法:“POST”, 标题:{ “内容类型”:“应用程序/json”, “X-CSRFToken”:csrf, },
这可能会对您有所帮助。是否有方法从fetch api重定向用户?它自己的fetch api没有重定向默认方法。请在我设置窗口时参考此@VishnuR。loaction=res.url如何传递数据?我正在重定向到仪表板,需要在仪表板中绘制的所有数据都应首先从数据库中获取,否则仪表板将不显示任何内容。
body : JSON.stringify(data)
}).then(result => result.json())
.then(response => {
if(response.status == 200){
window.location.href = '/'
}
else{
alert(response.message)
}
})