axios_instance.js 852 B

123456789101112131415161718192021222324252627282930
  1. const axios_instance = axios.create({
  2. baseURL: baseUrl + "mobile",
  3. timeout: 30000,
  4. headers: {'X-Requested-With':'XMLHttpRequest'},
  5. withCredentials: true,
  6. });
  7. axios_instance.interceptors.response.use(function (response) {
  8. const data = response.data;
  9. if (data.code === 1) {
  10. if (typeof error === 'function') {
  11. error(data.data);
  12. } else {
  13. Toast(data.msg);
  14. }
  15. } else if (data.code === 401) {
  16. location.href = '/mobile/login/login';
  17. }
  18. return response.data;
  19. });
  20. async function postJson(url,data) {
  21. return new Promise((resolve, reject) => {
  22. axios_instance.post(url, JSON.stringify(data), {
  23. headers: { 'Content-Type': 'application/json;charset=UTF-8' }
  24. }).then((result) => {
  25. return resolve(result.data);
  26. });
  27. });
  28. }