这可能看起来很愚蠢,但是当 Axios 中的请求失败时,我正在尝试获取错误数据。
axios
.get('foo.example')
.then((response) => {})
.catch((error) => {
console.log(error); //Logs a string: Error: Request failed with status code 404
});
除了字符串,是否有可能获得一个可能包含状态代码和内容的对象?例如:
Object = {status: 404, reason: 'Not found', body: '404 Not found'}
原文由 Sebastian Olsen 发布,翻译遵循 CC BY-SA 4.0 许可协议
你看到的是
error
对象的toString
方法返回的字符串。 (error
不是字符串。)如果从服务器接收到响应,则
error
对象将包含response
属性: