Axios 如何在 catch 里获取接口返回的错误信息

4,682 阅读1分钟

使用 axios 请求接口

axios(option).then(res => {
    console.log(res);
}).catch(err => {
    console.log(err);
})

接口返回 422,在 catch 里打印出来的 err 是:Error: Request failed with status code 422

没有接口返回来的错误信息

那么如何拿到接口返回来的错误信息呢?

err.response 可以拿到,即:

axios(option).then(res => {
    console.log(res);
}).catch(err => {
    console.log(err.response);
})