【发布时间】:2021-01-04 02:05:52
【问题描述】:
我正在尝试使用.then 类型的ajax 在登录后重定向我的页面。除了history.push() 命令,一切正常:
axios
.post('http://localhost:8080/login', {
email,
password
})
.then((res) => {
sessionStorage.token = res.data.token;
const { sub } = JSON.parse(atob(res.data.token.split('.')[1]));
sessionStorage.email = sub;
})
.then(() => history.push("/reservations"))
.catch(() => setError(connectionError))
}
它不是重定向,而是正确登录但不更改页面。刷新时页面会更新,但仍然没有重定向。 不确定这是否重要,但这是我的路线:
<Route path="/reservations" exact component={Reservations}/>
帮助非常感谢
【问题讨论】:
-
可能还有其他问题,但
.then(history.push("/reservations"))是错误的。应该是.then(() => history.push("/reservations"))。 -
嗨,我也面临同样的问题。您找到解决方案了吗?
标签: javascript reactjs ajax redirect browser-history