本问题基于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的有无.
两种设置有何不同,为什么导致如此结果,求解答