这可能看起来很愚蠢,但我试图在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'}
为了获得从服务器返回的http状态代码,你可以在axios选项中添加validateStatus: status => true:
axios({
method: 'POST',
url: 'http://localhost:3001/users/login',
data: { username, password },
validateStatus: () => true
}).then(res => {
console.log(res.status);
});
这样,每个http响应都会解析从axios返回的承诺。
https://github.com/axios/axios#handling-errors
为了获得从服务器返回的http状态代码,你可以在axios选项中添加validateStatus: status => true:
axios({
method: 'POST',
url: 'http://localhost:3001/users/login',
data: { username, password },
validateStatus: () => true
}).then(res => {
console.log(res.status);
});
这样,每个http响应都会解析从axios返回的承诺。
https://github.com/axios/axios#handling-errors