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

标签 javascript node.js

我想在 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/

相关文章:

javascript - 部署到 Heroku 期间出现扩展运算符语法错误

javascript - 用于检查 9 个字母单词中前两个字符必须为 "1 (numaric)-"的正则表达式,其余应为字母数字

javascript - 如何通过 BootstrapVue 仅使用 API 中所需的字段?

node.js - 如何在 node.js 上使用子进程运行 top 命令而不从终端获取 'stdout maxBuffer exceeded'

node.js - mongodb Node 崩溃,try catch 异常

node.js - 无法在heroku中使用socket io

javascript - 解析短连字符?

javascript - 加载 GeoJson Google API - CORS 政策

node.js - nodejs 将数据附加到读取流的末尾

javascript - html 表中的 Node.js Cheerio 解析 html 表