最近使用vue-cli脚手架前后端分离开发时,调用接口使用proxyTable代理实现跨域
在config/index.js的dev中加入:
proxyTable: {
'/apis': {
target: 'http://xxx.com', // 接口的域名
changeOrigin: true, // 如果接口跨域,需要进行这个参数配置
pathRewrite: {
'^/apis': '' //重定向或重新赋值
}
}
},
入口文件main.js中:
import axios from 'axios';
Vue.prototype.$axios = axios;
axios.defaults.baseURL = '/apis'
请求可解决跨域问题
this.$axios.get('/index').then((res) => {
console.log(res)
}).catch((res)=>{
console.log(res);
})
显示请求
http://localhost:8080/apis/index
实际请求
http://xxx.com/index