解决Vue前端请求后端解决接口跨域问题(开发环境下)

问题描述:在进行接口请求的时候跨域请求的错误。
在这里插入图片描述
解决方法:
1、修改config文件下的index.js里面的proxyTable属性:

proxyTable: {
      '/api':{
        target:'http://xxx.xx.xx.xx:8080',
        changeOrigin: true,
        pathRewrite:{
          '^/api':'/api'
        }
      }
    },

说明:proxyTable里面的’/api’为目标请求接口的后的url名称

2、vue.config.js文件修改proxy属性:

proxy: {
      // 匹配所有以 /api 开头的url
      '/api': {
        // 请求的目标主机
        target: 'http://xxx.xx.xx.xx:8080',
        changeOrigin: true,
        ws: true
        // 这样重写会把路径中 /api 消去
        pathRewrite: {
        '^/api': '/api'
        }
      }
    }

3、简单的进行axios接口请求:

this.$http.get('/api/xxxxx?xxxx=1路&xxx=0926&xxxx=10',{
          headers: {
            'Content-type': 'application/json;charset=UTF-8'
            /*'Content-Type': 'text/plain;charset=utf-8'*/
          },
        }).then(function (response) {
            console.log(response,'response');
          })
          .catch(function (error) {
            console.log(error);
          });

进行上述修改后,接口跨域请求问题得到解决。

4、生产环境下的跨域问题一般由后端解决(安全性问题)

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值