4 回答
TA贡献1827条经验 获得超9个赞
试试这个:
axios.post("http://localhost:4000/snackhub/login",
loginUser)
.then(res => {
},
error => {
}
);
TA贡献1818条经验 获得超8个赞
对于发送错误或您应该这样写的任何响应。
res.status(422).send({ success: false, error: "User already exist!" });TA贡献1799条经验 获得超8个赞
中的错误处理axios与其他的略有不同。在这里,您在块中获取response错误对象中的对象。catch我建议您记录响应,然后相应地进行处理。
一个例子是这样的:
axios.post(url, data).then(res => {
// do good things
})
.catch(err => {
if (err.response) {
// client received an error response (5xx, 4xx)
} else if (err.request) {
// client never received a response, or request never left
} else {
// anything else
}
})
TA贡献1900条经验 获得超5个赞
我遇到了同样的问题,我想出了解决这个问题的方法。你可以试试console.log(error.response.data)。这将打印从您的服务器发送的 JSON 响应...即:res.status({message: "Custom error message"}) 可以在客户端使用 axios 访问:
axios.get(url)
.then(response => {
// your logic
})
.catch(err => {
console.log(err.response.data.message)
})
添加回答
举报
