vue+axios跨域请求

2,452 阅读1分钟

发送请求报错信息

在做手机注册页面的时候,发送验证请求是正常的但是点击提交了就后台返回的信息是注册失败,后台人员说我每次传过来的session都不一样导致返回值是错误的。这你受得了。

我就在main.js加上这段代码 axios.defaults.withCredentials=true

import Vue from 'vue'
import axios from 'axios'
import App from './App'
import router from './router'
 
axios.defaults.withCredentials=true;

然后在注册页面上加上withCredentials: true

that.$axios({
        method: 'post',
        url: '',
        withCredentials: true
      }).then(res => {}

当我以为完成了以后,居然还是报错了,真是坑呀,又得加班 报的是跨域问题,那就找跨域呗

那就在vue新建一个vue.config.js文件在里面配置一下就可以了

devServer: {
    proxy: {
      '/apis': {
        target: 'http://test.xxxx.com',  // target host
        ws: true,  // proxy websockets
        changeOrigin: true,  // needed for virtual hosted sites
        pathRewrite: {
          '^/apis': ''  // rewrite path
        }
      },
    },
  }

终于走通了...