首页 > 其他 > 详细

使用proxyTable解决vue里的跨域问题

时间:2019-04-22 16:17:43      阅读:362      评论:0      收藏:0      [点我收藏+]

由于没有跨域的接口,所以,用8080端口请求8081端口,来模拟跨域。跨域会出现下面报错。

技术分享图片

 

1.找到config文件夹下index.js,在proxyTable对象里复制以下代码:

    proxyTable: {
      ‘/apis‘: {
        // 测试环境
        target: ‘https://localhost:8081‘,  // 接口域名
        changeOrigin: true,  //是否跨域
        pathRewrite: {
            ‘^/apis‘: ‘‘   //需要rewrite重写的,
        }              
    }
    },

 

 注意:【更改完这个配置文件后,需要重启项目】

 

2.使用fetch或者axios请求接口,【不能用vue-resource,vue-resource不生效】

fetch请求:

    //用 /apis代替http://localhost:8082
    fetch("/apis").then(res=>{
      console.log(res);
    })

 

axios请求:

this.$axios.get("/apis").then(res=>{
      console.log(res);
    })

 

这样请求,就不会跨域了:

技术分享图片

技术分享图片

使用proxyTable解决vue里的跨域问题

原文:https://www.cnblogs.com/luguankun/p/10750734.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!