axios.js 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. import axios from "axios";
  2. // import { gettoken } from "../services/token";
  3. // axios.defaults.withCredentials=true;
  4. const instance = axios.create({
  5. // baseURL: 'http://192.168.2.164:8899', //请求的基础地址
  6. // baseURL: window.location.origin + "/backend",
  7. baseURL: "https://scrm.zzliaoyuan.com/backend", //请求的基础地址
  8. timeout: 60000, //请求超时的时间
  9. });
  10. //发一个get请求
  11. export const get = (url, params) => instance.get(url, { params });
  12. //发一个post请求
  13. export const post = (url, data) => instance.post(url, data);
  14. export const put = (url, params) => instance.put(url, params);
  15. export default instance;
  16. // Add a request interceptor
  17. //全局请求拦截,发起网络请求之前执行
  18. instance.interceptors.request.use(
  19. function (config) {
  20. // Do something before request is sent
  21. // config.headers.Authorization = gettoken();
  22. return config;
  23. },
  24. function (error) {
  25. // Do something with request error
  26. return Promise.reject(error);
  27. }
  28. );
  29. // Add a response interceptor
  30. //全局响应拦截,网络请求返回之后执行
  31. instance.interceptors.response.use(
  32. function (response) {
  33. // Any status code that lie within the range of 2xx cause this function to trigger
  34. // Do something with response data
  35. return response.data;
  36. },
  37. function (error) {
  38. // Any status codes that falls outside the range of 2xx cause this function to trigger
  39. // Do something with response error
  40. return Promise.reject(error);
  41. }
  42. );