【发布时间】:2019-05-06 07:59:55
【问题描述】:
我希望能够从 Axios 错误捕获中获取响应正文。
我正在使用 axios v0.18.0。
我的 axios 代码如下所示:
let service = axios.create({
baseURL: "https://baseUrl.azurewebsites.net",
responseType: "json"
});
service.defaults.headers.common['authorization'] = `Bearer ${token}`;
service.post("/api/method", params).then(result => {
console.log('success',result);
}).catch(error=>{
console.log('error');
console.log(error);
});
根据我的输入,我的 API 调用返回 400 错误,正如我所料。所以我正在击中catch块。但是我无法检索 API 调用返回的错误消息。
我尝试过执行 console.out(error.response.data),但这返回 null。
我已使用 Postman 验证 API 调用确实在响应正文中返回错误消息,因此 API 不是问题。
我错过了什么?
【问题讨论】:
标签: javascript error-handling axios