Using Axios is there a way to “hook” a function to every post request?
I have a ‘notification’ module in Vue.js
store, which stores API responses, so I need to call an updateResponse
method in every post request .then
Advertisement
Answer
Just use Axios interceptors
JavaScript
x
10
10
1
axios.interceptors.response.use(function (response) {
2
// Any status code that lie within the range of 2xx cause this function to trigger
3
// Do something with response data
4
return response;
5
}, function (error) {
6
// Any status codes that falls outside the range of 2xx cause this function to trigger
7
// Do something with response error
8
return Promise.reject(error);
9
});
10