vue webpack-simple 解决跨域问题

npm install --save-dev http-proxy-middleware
  • //webpack.config.js
var proxy = require('http-proxy-middleware');

...............

  devServer: {
    historyApiFallback: true,
    noInfo: true,
    overlay: true,
    proxy: {
      '/api': {//匹配根路径
        target: 'https://******/',//跨域要访问的地址及端口
        changeOrigin: true,
      }
    },
  }
  • 用到的时候
    axios.get('/api/v1/dairy/list')
      .then(res => {
        // 成功回调
        console.log(res);
      }, res => {
        // 错误回调
        console.log(res);
      })

猜你喜欢

转载自www.cnblogs.com/wulzt/p/10460810.html