axios.get('/user/12345')
.catch(function (error) {
if (error.response) {
// The request was made and the server responded with a status code// that falls out of the range of 2xx
console.log(error.response.data);
console.log(error.response.status); // 状态码
console.log(error.response.headers);
} elseif (error.request) {
// The request was made but no response was received// `error.request` is an instance of XMLHttpRequest in the browser and an instance of// http.ClientRequest in node.js
console.log(error.request);
} else {
// Something happened in setting up the request that triggered an Error
console.log('Error', error.message);
}
console.log(error.config);
});
You can define a custom HTTP status code error range using the validateStatus config option. –自定义选项
axios.get('/user/12345', {
validateStatus: function(status) {
return status < 500; // Reject only if the status code is greater than or equal to 500
}
})