Ajax无法获取cookie
项目中用axios获取数据的时候获取不到cookie,按照网上的方法设置了无数次都不行,之后想用ajax做一个测试,前端代码如下
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,响应头如下
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
从响应头来看后台设置的Access-Control-Allow-Credentials: true是没问题的,搞不懂问题出在哪里,求大佬指点!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
AJAX获取COOKIE的话是要后端设置cookie并返回的
配置看着没毛病,我觉得还是先确定有cookie,并且检查cookie的
domain
、httponly
。