小编use*_*522的帖子

使用 Vue 处理来自 Express API 的错误

我在访问以 400 错误返回的 JSON 对象时遇到了一些困难。

我的 Vue 应用程序的响应只是错误 400,没有 JSON 数据。

Error: Request failed with status code 400
    at createError (createError.js?16d0:16)
    at settle (settle.js?db52:18)
    at XMLHttpRequest.handleLoad (xhr.js?ec6c:77)
Run Code Online (Sandbox Code Playgroud)

使用 Postman 的响应返回错误代码和消息。

如何在我的 Vue 应用程序中访问 JSON 响应?

{
    "error": "Email already exists!"
}
Run Code Online (Sandbox Code Playgroud)

快速API代码:

res.status(400).json({ error: 'Email already exists!' });

我的Vue代码:

handleSubmit () {
  this.$http.post('http://localhost:3000/users/create', this.user)
      .then(response => {
          const status = JSON.parse(response.status)
          if (status === 200) router.push('/users')
      })
      // why doesn't the json response show
      .catch(err => {
          console.log(err)
      })
}
Run Code Online (Sandbox Code Playgroud)

json node.js express vue.js axios

0
推荐指数
1
解决办法
2527
查看次数

标签 统计

axios ×1

express ×1

json ×1

node.js ×1

vue.js ×1