【发布时间】:2019-08-07 18:57:12
【问题描述】:
我在访问返回 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)
使用 Postman 的响应会返回错误代码和消息。
如何在我的 Vue 应用中访问 JSON 响应?
{
"error": "Email already exists!"
}
Express 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)
})
}
【问题讨论】:
标签: node.js json express vue.js axios