顺序:请求拦截,请求,响应拦截,响应,顺序不能颠倒
// Add a request interceptor
axios.interceptors.request.use(function (config) {
// Do something before request is sent
console.log('request is sent')
return config;
}, function (error) {
// Do something with request error
console.log("request error")
return Promise.reject(error);
});
axios.post('http://127.0.0.1:3000/posts')
.then(function (response) {
// handle success
console.log(response);
})
.catch(function (error) {
// handle error
console.log(error);
})
.then(function () {
// always executed
});
// Add a response interceptor
axios.interceptors.response.use(function (response) {
// Any status code that lie within the range of 2xx cause this function to trigger
// Do something with response data
console.log("response data successfully")
return response;
}, function (error) {
// Any status codes that falls outside the range of 2xx cause this function to trigger
// Do something with response error
console.log("response error")
return Promise.reject(error);
});
axios.get('http://127.0.0.1:3000/posts')
.then(function (response) {
// handle success
console.log(response);
})
.catch(function (error) {
// handle error
console.log(error);
})
.then(function () {
// always executed
});