鍍金池/ 問答/網(wǎng)絡(luò)安全  HTML/ Ajax無法獲取cookie

Ajax無法獲取cookie

項(xiàng)目中用axios獲取數(shù)據(jù)的時(shí)候獲取不到cookie,按照網(wǎng)上的方法設(shè)置了無數(shù)次都不行,之后想用ajax做一個(gè)測試,前端代碼如下

var x = new XMLHttpRequest()
x.withCredentials =true
x.open('GET', 'http://a.com');
x.setRequestHeader("lang", "zh");
x.onload=function(){
    console.log(x.responseText);
};
x.send();

還是無法獲取cookie,響應(yīng)頭如下

Access-Control-Allow-Credentials: true
Access-Control-Allow-Headers: x-requested-with, Content-Type,Cookie, Accept, multipart/form-data, application/x-www-form-urlencoded, lang, token, os, version, time
Access-Control-Allow-Methods: POST, GET, OPTIONS, PUT, DELETE
Access-Control-Allow-Origin: http://192.168.0.128:8020
Cache-Control: no-cache, must-revalidate, no-cache, private
Connection: close
Content-Type: application/json
Date: Thu, 06 Sep 2018 10:27:57 GMT
Pragma: no-cache
Proxy-Connection: keep-alive
Server: nginx/1.13.12
Transfer-Encoding: chunked
X-Content-Type-Options: nosniff
X-Powered-By: PHP/7.2.5

請求頭如下

lang: zh
Origin: http://192.168.0.128:8020
Referer: http://192.168.0.128:8020/test/new_file.html?__hbt=1536228082860
User-Agent: Mozilla/5.0 (Linux; Android 5.0; SM-G900P Build/LRX21T) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Mobile Safari/537.36

從響應(yīng)頭來看后臺(tái)設(shè)置的Access-Control-Allow-Credentials: true是沒問題的,搞不懂問題出在哪里,求大佬指點(diǎn)!

回答
編輯回答
情已空

AJAX獲取COOKIE的話是要后端設(shè)置cookie并返回的

2018年9月22日 23:34
編輯回答
厭惡我

配置看著沒毛病,我覺得還是先確定有cookie,并且檢查cookie的domain、httponly。

2017年5月21日 23:26