vue-cli开发环境前端跨域

config/index.js

proxyTable: {
      '/api1': {
        target: '******', changeOrigin: true, pathRewrite: {
          '^/api1': ''
        }
      }, '/api2': {
        target: '******', changeOrigin: true, pathRewrite: {
          '^/api2': ''
        }
      }, secure: false
    },

******  指你的请求接口

config/dev.js   测试环境搭建

'use strict'
const merge = require('webpack-merge');
const prodEnv = require('./prod.env');

module.exports = merge(prodEnv, {
  NODE_ENV: '"development"',
  API_HOST: ['"/api1"', '"/api2"']
});

 

config/prod.js   生产环境

'use strict';
module.exports = {
  NODE_ENV: '"production"', API_HOST: ['"/api1"', '"/api2"']
}

axios 设置 withcredentials:true

使用

this.$axios.get(process.env.API_HOST[3] + '/util/captcha', {responseType: 'arraybuffer'}).then(res => {
          if (res.status === 200) {
            this.captchaSrc = 'data:image/png;base64,' + btoa(new Uint8Array(res.data).reduce((data, byte) => data + String.fromCharCode(byte), ''))
          } else {
            this.$message(res.data.msg)
          }
        });

生产环境是需要后台做nginx反向代理(我们公司是这样的),然后又遇到本地环境session老是改变的问题,有人说是因为本地localhost和请求不再同一个域名下,withcredentials无效了。线上前端页面和后台接口在同一个域名下就没这个问题了。

记录别的解决方案:

https://blog.csdn.net/sky786905664/article/details/73920815

posted @ 2018-09-25 15:06  Merrys  阅读(367)  评论(0编辑  收藏  举报