Vue跨域解决

vue-cli项目中,解决跨域问题。

在config > index.js 文件中的proxyTable里边添加配置

proxyTable: {
      '/api': {  //使用"/api"来代替"http://f.apiplus.c"
        target: 'http://f.apiplus.cn', //源地址
        changeOrigin: true, //改变源
        pathRewrite: {
          '^/api': 'http://f.apiplus.cn' //路径重写
      }
   }
},

在vue组件中使用

this.axios({
     url: '/api/mall/servlet/json?funcNo=3040',
     // url: 'http://f.apiplus.cn/mall/servlet/json?funcNo=3040',
     method: 'get',
}).then(res => {
   console.log(res)
}).catch(error => {
   console.log(error)
})

需要重新运行项目,不然会报错

重新运行项目后本地开发就不会出现跨域问题了

猜你喜欢

转载自www.cnblogs.com/jiaxinzhu/p/12024479.html