【发布时间】:2018-11-16 09:00:07
【问题描述】:
即使响应包含 ajax 请求的“Set-Cookie”标头,cookie 也不会存储在浏览器中。
请求代码:
function hitLogin(){
var loginUrl = "http://myapp:8080/login";
var geturl;
$.ajax({
type: "GET",
url : loginUrl,
data: {
user : "user1",
password : "encryptedPassword"
},
headers: {
"credentials": 'include',
"withCredentials" : true,
"crossDomain": true,
"X-Requested-With" : "XMLHttpRequest",
"Content-type" : "application/x-www-form-urlencoded",
"Accept":"text/plain",
},
success : function(data)
{
alert("Ajax request data: "+data);
},
error: function( xhr, status, error )
{
alert("Ajax request error: "+status );
}
});
}
收到的响应标头:
访问控制允许凭据:true
Access-Control-Allow-Headers:X-Requested-With,accept,content-type,Cookie
访问控制允许方法:POST、GET、PUT、OPTIONS、DELETE
访问控制允许来源:http://myapp2.com:7011
访问控制最大年龄:3600
内容编码:gzip
Content-Type:text/plain;charset=ISO-8859-1
日期:格林威治标准时间 2018 年 6 月 6 日星期三 15:10:09
服务器:Apache-Coyote/1.1
设置-Cookie:MYCOOKIE=62lml5_S7qS31KaFDg-SH-e8Ds5FPjljCIHzfmhxMAr8Fdrqr6fHLjI7s2XPAO2P3tNFLNLS1_fgvDXF4pLmfg#1s1S1#normal-false; 路径=/; HttpOnly
传输编码:分块
变化:接受编码
withCredentials:true
当浏览器访问相同的 url 时,我可以看到存储在浏览器中的 cookie,但在 ajax 请求的情况下,它不会存储。 因此无法发送除此 cookie 之外的后续请求。
【问题讨论】:
标签: jquery ajax google-chrome setcookie jquery-cookie