解决vue+webpack项目接口跨域问题

1、config文件夹下index.js文件设置proxyTable(proxyTable后面的host可以设置也可以保持默认的localhost)

proxyTable: {
  '/api': {
    target: 'http://10.xx.xx.xx:8080/renter-server', // 开发环境
    // target: 'http://10.xx.xx.xx:8080/renter-server', // 生产环境接口
    changeOrigin: true,
    pathRewrite: {
      '^/api': '/'
    }
  }
},
host: '自己的IP或者默认的localhost', // can be overwritten by process.env.HOST
  1. 在使用axios请求的时候把所有接口的'http://10.xx.xx.xx:8080/renter-server'部分替换为'/api'

例如我们项目里axios的配置文件夹(自定义的)api下面的index.js文件里有如下配置

let env = process.env.NODE_ENV
let BASE_1, BASE_2
if (env === 'production') {// 生产环境 正式打包使用
  BASE_1 = BASE_2 = CONFIG.apiHost
} else if (env === 'development') {// 开发环境 本地测试使用
  BASE_1 = BASE_2 = '/api'
}

再用BASE1拼接进行请求

axios.get(BASE_1 + '/pc/getHouseList', {params: params})

猜你喜欢

转载自blog.csdn.net/liona_koukou/article/details/83829711