1
0

axios-init.js 915 B

123456789101112131415161718192021222324252627282930
  1. axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded; charset=UTF-8';
  2. axios.defaults.headers.common['X-Requested-With'] = 'XMLHttpRequest';
  3. axios.interceptors.request.use(
  4. (config) => {
  5. if (config.data instanceof FormData) {
  6. config.headers['Content-Type'] = 'multipart/form-data';
  7. } else {
  8. config.data = Qs.stringify(config.data, {
  9. arrayFormat: 'repeat',
  10. });
  11. }
  12. return config;
  13. },
  14. (error) => Promise.reject(error),
  15. );
  16. axios.interceptors.response.use(
  17. (response) => response,
  18. (error) => {
  19. if (error.response) {
  20. const statusCode = error.response.status;
  21. // Check the status code
  22. if (statusCode === 401) { // Unauthorized
  23. return window.location.reload();
  24. }
  25. }
  26. return Promise.reject(error);
  27. }
  28. );