我有一个ajax调用,最后我有一个'then'承诺,其中我可以看到控制台日志在出现错误时返回false但在下一行中似乎没有评估if条件.
我已将下面代码中的条件评为"未评估".
请参考以下代码:
authenticate(email, password){
const encodedEmail = encodeURIComponent(email);
console.log(encodedEmail);
axios.get(`http://localhost:8080/api/users/${encodedEmail}`,{
headers: {
'Access-Control-Allow-Origin': '*',
Authorization: 'Basic ' + btoa(email + ":" + password)
}
})
.then(function (response) {
console.log(response);
sessionStorage.setItem('isAuthenticated', true);
})
.catch(function (error) {
console.log(error);
sessionStorage.setItem('isAuthenticated', false);
}).then(() => {
console.log(sessionStorage.getItem('isAuthenticated')); //returns true as expected
!sessionStorage.getItem('isAuthenticated')
&& this.props.history.push("/"); // not evaluated
});
}
handleLogIn(e){
e.preventDefault();
const email = e.target.elements.email.value;
const password = e.target.elements.password.value;
this.authenticate(email, password);
this.props.dispatch(addCredentials({email, password}));
}
Run Code Online (Sandbox Code Playgroud)
这是我第一次动手项目,请帮忙!提前致谢.