vue axios发的post请求没携带cookie

没法携带Cookie至后端

我用的是vuecli3版本,发post请求,用了网上的axios.defaults.withCredentials=true依然无济于事

登陆时,后台能收到数据,但是rightCode=null(就是存在我本地cookie里的验证码),导致一直返回验证码错误。

相关代码

main.js中

import axios from "axios";
import VueAxios from "vue-axios"
axios.defaults.withCredentials=true;
Vue.use(VueAxios,axios)

login.vue组件中

submit(){    
        this.axios.post('/user/login', 
        {
        account: this.account,
        password: this.password,
        checkCode: this.checkCode
        },{
            headers: {"Content-Type":"application/json;charset=utf-8"},
            // withCredentials:true
      }).then(function (response) {
            console.log(response.data);
        })
        .catch(function (error) {
            console.log(error);
        });

}

摆脱各位伸出援手,我是真的顶不住了。不知道我这样描述是否清楚,不清楚可以继续问我,真心求解,两天了困扰我

阅读 8.2k
4 个回答

F12检查下,确定时COOKIE发不过去?这个问题我之前也遇到过,也很有可能是后端的问题。

withCredentials 需要后端配合,在 Response Header 中返回如下。

Access-Control-Allow-Credentials: true
  1. 不跨域的时候,HTTP 携带 cookie 是协议里规定的,换言之,你不需要主动操作
  2. 跨域的时候,如果服务器包含 CORS 头,那么 withCredentials=true 可以携带 cookie。
  3. 看不出来有任何必要把验证码存到 cookie 里面,程序逻辑有问题
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题