vue-cli配置proxyTable 跨域请求

20次阅读

共计 467 个字符,预计需要花费 2 分钟才能阅读完成。

本地 express 服务启动端口 3000,vue-cli 项目启动端口 8080,当前端用户提交登录信息到后端,便产生跨域,可以配置 proxyTable 解决跨域问题一:进入 vue-cli 项目下的 config/index.js 文件,添加
proxyTable: {
‘/api’: {
target: ‘http://xxxxxx.com’, // 接口的域名
// secure: false, // 如果是 https 接口,需要配置这个参数
changeOrigin: true, // 如果接口跨域,需要进行这个参数配置
pathRewrite: {
‘^/api’: ”
}
}
},

此处的 target 设为 ‘http://localhost:3000/’

二:在前端发送请求页面,设置请求 url
this.$axios.post(‘/api/checklogin’,{
username:that.ruleForm.username,
password:that.ruleForm.password
})

三 启动本地 node 服务,启动 vue 项目,发送请求数据,控制台查看,获取数据成功则配置正确

正文完
 0