在使用 vue-cli3.0 结合 axios 请求后台多个 server, 遇到跨域问题,进行以下几个步骤的改动,就可解决
1、vue.config.js
devServer: {
open: true,
port: 8081,
proxy: {
'/api': {
target: process.env.TARGET1,
changeOrigin: true,
pathRewrite: {'^/api': '',},
logLevel: 'debug',
},
'/ips': {
target: process.env.TARGET2,
changeOrigin: true,
pathRewrite: {'^/ips': '',},
logLevel: 'debug',
},
},
},
2、.env
TARGET1= "http://10.50.60.100:8088"
TARGET2= "http://10.50.60.100:50050"
3、api
export function test1(data) {
return request({
url: 'api/test1',
method: 'post',
data,
});
}
export function test2(data) {
return request({
url: 'ips/test2',
method: 'post',
data,
});
}