引入axios
npm install axios
配置proxy代理,解决跨域问题
proxyTable: {
"/api": {
target: "http://192.168.X.XXX:XXXX", //需要跨域的目标
pathRewrite: { "^/api": "" }, //将带有api的路径重写为‘’
ws: true, //用与支持webCocket
changeOrigin: true //用于控制请求头的Host
},
"/two": {
target: "http://XXX.XXX.X.XXX:XXXX",
pathRewrite: { "^/two": "" },
ws: true,
changeOrigin: true
}
},
引入axios,二次封装,添加请求、响应拦截器
import axios from "axios";
const requests = axios.create({//创建
baseURL: "/api", //在调用路径中追加前缀‘/api’
timeout: 50000 //单位ms,超过该时间即为失败
});
//添加请求拦截器
requests.interceptors.request.use(
function(config) {
config.headers.token ="token";//在发送请求之前的行为,加入token
return config;
},
funct