javascript - 未捕获( promise )TypeError : Cannot set property 'status'

标签 javascript json ajax vue.js axios

嘿,我在尝试使用 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/

相关文章:

javascript - 使用 AJAX 登录,其中数据位于文本文件中

javascript - 改变滚动时html的内容

javascript - Express.js POST 方法不起作用

java - Android (Java) JSON 被发送到服务器,但里面的数据不是 "seeing",而是对象的 toString

c# - 带有 Newtonsoft 的 ASP.NET MVC 将对象从 Controller 传递到 View

javascript - Ajax 调用在移动设备中返回 0 作为响应

javascript - 从文本中解析坐标

javascript - 使用键盘快捷键捕获选定的文本

java - 读取 API 响应的通用代码

ajax - 请求的资源上不存在 'Access-Control-Allow-Origin' header