小编raj*_*aan的帖子

为什么'if'条件没有在ajax调用的'then'承诺中得到评估?

我有一个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)

这是我第一次动手项目,请帮忙!提前致谢.

javascript ajax callback promise reactjs

1
推荐指数
2
解决办法
58
查看次数

标签 统计

ajax ×1

callback ×1

javascript ×1

promise ×1

reactjs ×1