首页 > 解决方案 > 如何将用户重定向到另一个网页

问题描述

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

我做了一个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
})

标签: javascriptreactjs

解决方案


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

例如:

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

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


推荐阅读