vue2 利用网络代理axios实现前端跨域
vue.config.js 配置代理
module.exports = {
devServer: {
proxy: {
'/CorsDemo': {
target: 'http://usercenter-server.test.za-tech.net/',
changeOrigin: true,
pathRewrite: {
"^/api": "/",//重写匹配的字段。把/api 转为 /
}
},
}
},
}
vue文件
import axios from 'axios'
methods:{
functionA (){
axios.get('/CorsDemo/validate?service=' + '123').then(
response => {
console.log(response)
},
error => {'请求失败',error.message}
)}
}
实现开发环境跨域请求
测试生产环境跨域跳转
https://blog.csdn.net/weixin_43858851/article/details/137518111?csdn_share_tail=%7B%22type%22%3A%22blog%22%2C%22rType%22%3A%22article%22%2C%22rId%22%3A%22137518111%22%2C%22source%22%3A%22weixin_43858851%22%7D