【问题标题】:how to redirect User to another web-page如何将用户重定向到另一个网页
【发布时间】:2019-08-10 07:16:19
【问题描述】:

我的 RestAPI 服务器将用户请求重定向到另一个网页(例如 google.com)我如何将我的前端重定向到该页面,我的意思是我有一个按钮,当我按下它时会向我的服务器。我的服务器将其重定向到另一个网页,我想向我的客户显示该网页

我做了一个axios

 const url = "https://cors-anywhere.herokuapp.com/http://147.13.121.244:4001/redirect"
const client = axios.create({
  headers: {
    'Content-Type': 'application/json',
    'Authorization': 'Basic a3p0ZXN5547asdhMnFUdVZN',

  }
});

let result = await client.get(url, {}).then((response) => {
  return response
}).catch((error) => {
  return error
})

【问题讨论】:

  • 只从服务器获取url,然后window.location = newUrl
  • 是您应用的页面部分还是在其他主机上?

标签: javascript reactjs


【解决方案1】:

您可以使用window.location 将当前页面重定向到您从服务器接收到的 URL,或使用window.open 在另一个选项卡中打开该页面。

例如:

const redirectURL = // result.redirect
// Redirect
window.location = redirectURL;
// New Window
window.open(redirectURL, "_blank");

您必须检查 result 对象以确定如何准确获取重定向 URL,但是一旦您拥有它,这些将起作用。

【讨论】:

  • 问题是我需要在重定向之前设置基本身份验证,如果我要让 window.location = result.url;我猜它没有设置标题
  • 重定向位置是您应用的一部分还是在其他主机上?
  • Redirect 会执行一个新的 http 请求,你的 header 会丢失。
  • @HastaTamang 我有一个 url ("google.com") 我想将我的客户端移动到该 url 并且我需要我的用户在 Header 中获得授权,我该如何解决这个问题
  • 使用query stringsession
猜你喜欢
  • 1970-01-01
  • 2021-05-10
  • 1970-01-01
  • 2021-06-09
  • 2012-04-05
  • 2015-12-07
  • 2021-10-28
  • 1970-01-01
  • 2023-03-29
相关资源
最近更新 更多