import axios from "axios"; import Qs from "qs"; // axios.defaults.headers.post[‘Content-Type‘] = ‘application/json‘; const instance = axios.create({ // baseURL:process.env.VUE_APP_BASE_URL, baseURL: window.g.SERVICE_PATH, timeout: 50000, headers: { "Content-Type": "application/x-www-form-urlencoded; charset=UTF-8", }, transformRequest: [ function(data) { //允许向服务器发送前,修改稿请求数据 return Qs.stringify(data); //对data进行转换 }, ], }); //添加请求拦截器,在请求响应被then或catch处理前拦截它们 instance.interceptors.request.use( (config) => { //在发送请求之前做些什么 const token = window.localStorage.getItem("t"); if (token) { config.headers.common["Token"] = token; } else { } return config; }, (error) => { console.log(error); } ); //添加响应拦截器 instance.interceptors.response.use( (response) => { if (response.data.info) { Message.success(response.data.info); } return response.data; }, (error) => { console.log("err" + error); return Promise.reject(error); } ); export default instance;
原文:https://www.cnblogs.com/hq2020/p/14910284.html