失眠网,内容丰富有趣,生活中的好帮手!
失眠网 > Vue-cli配置代理服务器 解决跨域请求问题

Vue-cli配置代理服务器 解决跨域请求问题

时间:2019-06-26 15:23:10

相关推荐

Vue-cli配置代理服务器 解决跨域请求问题

vue.config.js配置文件中加上devServer配置项

proxy里面的’/demo’是进入代理服务器的标识,target要请求的跨域路径,pathRewrite是访问路径的时候将路径标识删除

module.exports = {pages: {index: {//入口entry: 'src/main.js',},},lintOnSave:false, //关闭语法检查//开启代理服务器(方式一)/* devServer: {proxy: 'http://localhost:5000'}, *///开启代理服务器(方式二)devServer: {proxy: {'/demo': {target: 'http://localhost:5001',pathRewrite:{'^/demo':''},// ws: true, //用于支持websocket// changeOrigin: true //用于控制请求头中的host值}}}}

在需要访问跨域请求的ajax访问地址加上进入代理服务器的标识demo就能实现跨域请求了

methods: {getCars(){axios.get('http://localhost:8080/demo/cars').then(response => {console.log('请求成功了',response.data)},error => {console.log('请求失败了',error.message)})}},

如果觉得《Vue-cli配置代理服务器 解决跨域请求问题》对你有帮助,请点赞、收藏,并留下你的观点哦!

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。