axios.js 1.4 KB

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