javascript - 如何在收到获取 POST 请求的响应后将用户重定向到页面?

标签 javascript node.js redirect fetch-api

我正在为使用 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/

相关文章:

javascript - Blueimp 画廊用于 Bootstrap 多个画廊问题

javascript - 可以用 Node 而不是 meteor 启动 meteor 应用程序吗?

node.js - 无法通过 npm 安装 node-sass 包

php - 表单提交后重定向的正确方法

javascript - 在 SugarCRM editview 中添加自定义 jQuery 验证

javascript - 如何在单击取消按钮时停止/暂停嵌入?

Node .js。获取 HTTP 查询的时间

java - 如何在socket io和android聊天应用程序中检查用户是否在线

python - DJANGO - 使用数据从 POST 重定向到不同的页面

redirect - Nginx 重写问号