用axios发送请求,接口返回的status不是200,而是其他的状态值比如400,401等;则在catch的时候,返回的error并不是一个对象,而是这样的字符串:
Error: Request failed with status code 401
at FtD3.e.exports (createError.js:16)
at e.exports (settle.js:17)
at XMLHttpRequest.w (xhr.js:66)
但是,在浏览器响应中有返回结果,在代码中却不能捕获到,如下
那么怎么获取这个返回值呢;
用error.response来接收error.response返回的就是这个错误对象,如下:
axios.get('/user/12345')
.then((res) => {
})
.catch((error) => {
console.log(error.response)
if (error.response) {
// 请求已发出,但服务器响应的状态码不在 2xx 范围内
This.$message.error(error.response.data.msg)
} else {
// Something happened in setting up the request that triggered an Error
console.log('Error', error.message);
}
})