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