跨域就是因为同源策略。为了安全的。
这里的配置跨域本质上就是将本地的地址转换成服务器需要的真正期望的地址。
本地地址 http://localhost:8080/api/allin/policy/getProductInfo
服务器 http://www.test.com/allin/policy/getProductInfo
devServer: {
proxy: {
'/api': {
target: 'http://www.test.com', //会替换axios的baseURL
ws: true,
changOrigin: true,
pathRewrite: {
'^/api': '' //重写
}
}
}
}
本地 http://localhost:8080/allin/getProductInfo
服务器 http://www.test.com/allin/policy/getProductInfo
devServer: {
proxy: {
'/allin': {
target: 'http://www.test.com',
ws: true,
changOrigin: true,
pathRewrite: {
'^/allin': '/allin/policy'
}
}
}
}
本地 http://localhost:8080/allin/getProductInfo
服务器 http://www.test.com/allin/policy/getProductInfo
devServer: {
proxy: {
'/allin': {
target: 'http://www.test.com/allin/policy',
ws: true,
changOrigin: true,
pathRewrite: {
'^/allin': '/'
}
}
}
}
通过这三个例子应该就能很清楚跨域的基本配置了