vue.config.js 处理axios跨域请求(反向代理)

请求如下:

this.axios.post('/consulttag/list', {
        appId: '6BB0724D-096E-7747-A7F3-ECA72A078E03'
      })
        .then(function(res) {
          console.log(res);
        })
        .catch(function(err) {
          console.log(err);
        });

 

然后在vue.config.js 里完整配置如下:

// const path = require('path')
module.exports = {
  publicPath: './',
  devServer: {
    proxy: {
      '/*': { //匹配路径
        target: 'http://192.168.8.110:8080',
        changeOrigin: true// 允许websockets跨域
        // ws: true,
      }
    }
  }
};

此时在本地运行时可以看见控制台的请求地址为http://localhost:8080//consulttag/list,然而实际请求路径为http://192.168.8.110:8080/consulttag/list

 

posted @ 2021-12-28 12:05  不如饲猪  阅读(531)  评论(0)    收藏  举报