vue proxyTable 接口跨域请求调试(代理)

注明:本方法是在自己的浏览器创建一个服务器,然后让自己的服务器区请求目标服务器,同时说明,跨域是针对js来说的,其实基本上是追对ajax来说的,因此服务器之间可以随便请求数据而不受限制。上面我们的服务器已经区目标服务器请求了,然后我们去请求我们自己的服务器,这是就不是跨域了,因此可以访问。下面的方法原理就是如此:

在不同域之间访问是比较常见,在本地调试访问远程服务器。。。。这就是有域问题。

VUE解决通过proxyTable:

在 config/index.js 配置文件中

dev: {
    env: require('./dev.env'),
    port: 8080,
    autoOpenBrowser: true,
    assetsSubDirectory: 'static',
    assetsPublicPath: '/',
    //proxyTable: {},
    proxyTable: proxyConfig.proxyList, //红线部分
    // CSS Sourcemaps off by default because relative paths are "buggy"
    // with this option, according to the CSS-Loader README
    // (https://github.com/webpack/css-loader#sourcemaps)
    // In our experience, they generally work as expected,
    // just be aware of this issue when enabling this option.
    cssSourceMap: false
  }

划红线部分就是设置代理参数:

在config目录创建,proxyConfig.js 写入

module.exports = {
  proxyList: {
        '/apis': {
            // 测试环境
            target: 'https://goods.footer.com',  // 接口域名
            changeOrigin: true,  //是否跨域
            pathRewrite: {
                '^/apis': ''   //需要rewrite重写的,
            }              
        }
  }
}

在 config/index.js 配置文件上边引入

var proxyConfig = require('./proxyConfig')

注明:上面代码可以放在一个文件中,即如下所示:

dev: {
    env: require('./dev.env'),
    port: 8080,
    autoOpenBrowser: true,
    assetsSubDirectory: 'static',
    assetsPublicPath: '/',
    //proxyTable: {},
    proxyTable: {
        '/apis': {
            // 测试环境
            target: 'https://goods.footer.com',  // 接口域名
            changeOrigin: true,  //是否跨域
            pathRewrite: {
                '^/apis': ''   //需要rewrite重写的,
            }              
        }
    },
    // CSS Sourcemaps off by default because relative paths are "buggy"
    // with this option, according to the CSS-Loader README
    // (https://github.com/webpack/css-loader#sourcemaps)
    // In our experience, they generally work as expected,
    // just be aware of this issue when enabling this option.
    cssSourceMap: false
  }

接着往下说,例如我们要请求一个这样的服务器借口,例:https://goods.footer.com/health/list
如何使用上面的方法呢,下面是vue代码

var obj = {
   pageSize: 20
}
this.$http.get( '/apis/health/list',{params: obj})
            .then(function(res){
       // 成功回调
            },function(){
              alert("error")
            })

另外再附上一个账号登陆提交的例子,如下:

submitForm:function (formName) {
  var info={
    user_phone: this.info.username,
    user_pwd: this.info.password,
    verify_code: this.info.password
  }
  var  that=this;
    this.$http.post('/list/login',info).then(function (res) {
        console.log(res.data)
          if(res.data.code==2000){
              this.$router.push({ path: 'OrderTracking'})

          }else (res.data.code==4000){
                alert("登陆失败")
          }   
 })
}

项目上线时后台配置
商家管理员端

server {
        listen       80;
        server_name  xxxx.xxxx.xxxx.com;
        #社区端
        location /list/ {
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header X-Nginx-Proxy true;
            proxy_set_header Connection "";
            proxy_pass https://api.xxxx.xxx.com/seller/;
        }
        location /ui/ {
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header X-Nginx-Proxy true;
            proxy_set_header Connection "";
            proxy_pass https://api.xxxx.xxx.com/public/;
        }
        location / {
            root   /www/xxx/xxx;
            index  index.html index.htm;
        }
    }
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值