vue.config.js配置反向代理
首先找到vue.config.js文件,然后在如下位置写入
module.exports = {
devServer: {
port: 8090,
proxy:{
"/api": {
// 接口请求路径
target: "http://xx.xxx.xxx.xx:xx",//反向代理的目标地址
changeOrigin: true
}
},
}
}
然后在页面上发送请求时就可以直接这样写
axios({
url: "/api/use/userInfo",//这里会把地址直接反向代理为http://xx.xxx.xxx.xx:xx/api/use/userInfo
data:{param1:"1"},
method: "post",
headers: {
"Content-Type": "application/x-www-form-urlencoded",
},
}).then(res=>{
if(res.status==200){
console.log(res)
}
})