嘿,我在尝试使用 Axios 和 VueJS 来确认从服务器收到的响应时遇到了一些问题
axios.post('/login', {
email: this.email,
password: this.password
}).then(response => {
if (response.status == 200) {
$root.$data.auth = true;
} else {
alert('uhhhh');
}
}).catch(e => {
if (e.response.status == 422) {
this.error = "You made that login right? How could you habve forgotten it!";
} else {
this.error = "Something else happened and this is what: " + e.response.status;
}
});
成功登录后,我会在控制台中看到以下内容:
未捕获( promise 中)类型错误:无法设置未定义的属性“状态”
这似乎令人困惑,因为 Axios 文档指出我可以提取 response.status 对象并从中运行条件逻辑?
最佳答案
无论如何你都不应该检查响应状态,因为如果不是 200(OK),代码将执行下一个回调,即 .catch()
,在这里你可以处理错误。
关于javascript - 未捕获( promise )TypeError : Cannot set property 'status' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47500649/