概述
Vue与node.js项目中遇到的跨域问题,记录下解决方案:
在项目中我的node提供的api为:
127.0.0.1:3000/toutiao/seltoutiaoimg |
众所周知,Vue默认端口为8080,故详情求node.js的API需要通过跨域实现
Vue中也提供了设置跨域的文件,下面是例子:
设置地址方式如下:
位置:config->index.js中
proxyTable: { '/api': { target: 'http://127.0.0.1:3000', changeOrigin: true, pathRewrite: { '^/api': '' } } }, |
这里的/api被映射为http://127.0.0.1:3000
Vue中的请求:
mounted: function () { this.$http.get('/api/toutiao/seltoutiaoimg', {}, { },{}).then(function (response) { // 这里是处理正确的回调 console.log(response) // this.articles = response.data["subjects"] 也可以 }, function (response) { // 这里是处理错误的回调 console.log(response) }); } |
可以看到我们的URL地址
会被映射为:http://127.0.0.1:3000/tutiao/seltoutiaoimg
即可解决跨域最后
以上就是秀丽面包为你收集整理的Vue设置proxyTable实现跨域的全部内容,希望文章能够帮你解决Vue设置proxyTable实现跨域所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复