我正在使用Nodejs后端返回OAuth的自定义重定向URL。我正在尝试让React将用户重定向到此URL,但是它将localhost添加到它的前面。
import React, { Component } from "react";
import { withRouter } from "react-router";
import axios from "axios";
class Test extends Component {
async componentDidMount() {
const profileData = await axios.get("/connect");
this.props.history.push(profileData.data);
}
render() {
return <div />;
}
}
export default withRouter(Test);
因此,假设返回的profileData.data字符串为http://www.google.com,则网页将重定向到http://localhost:3000/http://www.google.com。
是否可以使用React进行重定向以使其重定向到特定的URL,而不是尝试重定向到子页面?
答案 0 :(得分:1)
只需在您的componentDidMount函数中放置一个位置href。我会这样写:
componentDidMount() {
axios.get("/connect")
.then((response) => {
window.location.href = response.data
})
}