gpt4 book ai didi

javascript - 如何在 fetch() 函数之后重定向

转载 作者:行者123 更新时间:2023-12-02 22:40:46 25 4
gpt4 key购买 nike

我想在 fetch() 函数之后将用户重定向到新网页。我使用 Facebook 登录按钮,当用户通过身份验证时,我想将他重定向到包含他的数据的新页面。所以我想知道最好的方法是什么。

我认为 window.location + session 变量可以工作,但我很确定有比此方法更正确的方法,例如 then() 函数之后的重定向函数。

我厌倦了使用 fetch().then().redirect() 但它不起作用,我想做的是像 ...().redirect(function(userDatas, url){.. .});

    //this is inside the facebook connection function
fetch('/connect',options).then(function(response){
response.json().then(function(value){
window.location = 'http://localhost:3000/fight.html';
console.log(value);
});

//after this function, I would like to go to this fight.html page

所以我希望用户在FB连接后到达fight.html页面及其数据。

最佳答案

你的做法是错误的。您正在对 response.json() 的响应调用 .then() 而不是主 promise 链。尝试:

fetch('/connect',options)
.then(function(response) {
return response.json();
})
.then(function(value) {
window.location = 'http://localhost:3000/fight.html';
console.log(value);
});

关于javascript - 如何在 fetch() 函数之后重定向,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58595026/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com