公孙杰 2020-08-11 01:38 采纳率: 0%
浏览 1113

axios在单独的请求设置withCredentials:'true'和在main.js设置axios.defaults.headers['withCredentials'] = true;不同

本问题基于vue和django的前后端分离跨域问题

在单个请求设置withCredentials:'true'

userLogin(){
            this.$axios({
                method: 'post',
                url: '/test/loginJson',
                data:{
                    'number': this.number,
                    'passwd': this.passwd,
                    'radio': this.radio,
                },
                withCredentials: 'true',
            })
            .then(res=>{
                console.log(res.data);
                if(res.data.state){
                    this.setCookie('sessionid',res.data.session_id)
                }
            })
            .catch(err=>{
                console.error(err);
            })
        },

和在main.js中设置axios.defaults.headers['withCredentials'] = true;

axios.defaults.baseURL='http://127.0.0.1:8000'
axios.defaults.headers['withCredentials'] = true; // 跨域资源访问
Vue.prototype.$axios = axios;

为何产生的结果不同。

在单独请求中,django的seesion不会丢失,而使用全局默认设置获取不到session。

单独设置报文

Request URL: http://127.0.0.1:8000/test/loginJson
Request Method: POST
Status Code: 200 OK
Remote Address: 127.0.0.1:8000
Referrer Policy: no-referrer-when-downgrade
Access-Control-Allow-Credentials: true
Access-Control-Allow-Origin: http://127.0.0.1:8080
Content-Length: 65
Content-Type: text/html; charset=utf-8
Date: Mon, 10 Aug 2020 17:07:30 GMT
Referrer-Policy: same-origin
Server: WSGIServer/0.2 CPython/3.8.2
Set-Cookie: sessionid=ptxlhgdz1684js1cqh9lgt6iluy1gf82; expires=Mon, 24 Aug 2020 17:07:30 GMT; HttpOnly; Max-Age=1209600; Path=/; SameSite=Lax
Vary: Origin, Cookie
X-Content-Type-Options: nosniff
X-Frame-Options: DENY
Accept: application/json, text/plain, */*
Accept-Encoding: gzip, deflate, br
Accept-Language: zh-CN,zh;q=0.9
Cache-Control: no-cache
Connection: keep-alive
Content-Length: 36
Content-Type: application/json;charset=UTF-8
Host: 127.0.0.1:8000
Origin: http://127.0.0.1:8080
Pragma: no-cache
Referer: http://127.0.0.1:8080/
Sec-Fetch-Dest: empty
Sec-Fetch-Mode: cors
Sec-Fetch-Site: same-site
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.105 Safari/537.36
withCredentials: true

{number: "", passwd: "", radio: ""}
number: ""
passwd: ""
radio: ""

全局设置的报文

Request URL: http://127.0.0.1:8000/test/loginJson
Request Method: POST
Status Code: 200 OK
Remote Address: 127.0.0.1:8000
Referrer Policy: no-referrer-when-downgrade
Access-Control-Allow-Credentials: true
Access-Control-Allow-Origin: http://127.0.0.1:8080
Content-Length: 65
Content-Type: text/html; charset=utf-8
Date: Mon, 10 Aug 2020 17:08:27 GMT
Referrer-Policy: same-origin
Server: WSGIServer/0.2 CPython/3.8.2
Set-Cookie: sessionid=lrx6mf4rqxgjmpvb8yycn8610tbhe5sr; expires=Mon, 24 Aug 2020 17:08:27 GMT; HttpOnly; Max-Age=1209600; Path=/; SameSite=Lax
Vary: Origin, Cookie
X-Content-Type-Options: nosniff
X-Frame-Options: DENY
Accept: application/json, text/plain, */*
Accept-Encoding: gzip, deflate, br
Accept-Language: zh-CN,zh;q=0.9
Cache-Control: no-cache
Connection: keep-alive
Content-Length: 39
Content-Type: application/json;charset=UTF-8
Cookie: sessionid=lrx6mf4rqxgjmpvb8yycn8610tbhe5sr
Host: 127.0.0.1:8000
Origin: http://127.0.0.1:8080
Pragma: no-cache
Referer: http://127.0.0.1:8080/
Sec-Fetch-Dest: empty
Sec-Fetch-Mode: cors
Sec-Fetch-Site: same-site
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.105 Safari/537.36

{number: "123", passwd: "", radio: ""}
number: "123"
passwd: ""
radio: ""

两者区别在Cookie的有无.

两种设置有何不同,为什么导致如此结果,求解答

  • 写回答

1条回答 默认 最新

  • dabocaiqq 2020-08-11 10:28
    关注
    评论

报告相同问题?

悬赏问题

  • ¥500 把面具戴到人脸上,请大家贡献智慧
  • ¥15 任意一个散点图自己下载其js脚本文件并做成独立的案例页面,不要作在线的,要离线状态。
  • ¥15 各位 帮我看看如何写代码,打出来的图形要和如下图呈现的一样,急
  • ¥30 c#打开word开启修订并实时显示批注
  • ¥15 如何解决ldsc的这条报错/index error
  • ¥15 VS2022+WDK驱动开发环境
  • ¥30 关于#java#的问题,请各位专家解答!
  • ¥30 vue+element根据数据循环生成多个table,如何实现最后一列 平均分合并
  • ¥20 pcf8563时钟芯片不启振
  • ¥20 pip2.40更新pip2.43时报错