带域域的CORS cookie仅在使用jQuery AJAX的Firefox中设置

时间:2012-12-19 13:54:43

标签: ajax jquery cookies cors

使用跨网站请求添加domain字段时,我无法设置Cookie。我试图通过jquery ajax调用请求来实现这一点。

是否可以让它在除Firefox之外的其他浏览器中运行?

有些请求标题:

Accept:application/json, text/javascript, */*; q=0.01
Content-Length:55
Content-Type:application/x-www-form-urlencoded; charset=UTF-8
Host:localhost:53862
Origin:http://localhost:54265
Referer:http://localhost:54265/

响应标题:

Access-Control-Allow-Credentials:true
Access-Control-Allow-Headers:x-requested-with, origin, content-type, accept, Proxy-Connection
Access-Control-Allow-Methods:GET,POST,PUT,OPTIONS, DELETE
Access-Control-Allow-Origin:http://localhost:54265
Set-Cookie:Auth=l_hash=123456&user=xyzl&remember_me=false; expires=Fri, 18 Jan 2013 13:42:10 GMT; domain=localhost; path=/

代码:

$.ajax({
    type: "PUT",
    url: apiHost + "api/account/login/",
    data: $("#loginBarForm").serialize(),
    dataType: "json",
    contentType: "application/x-www-form-urlencoded; charset=UTF-8",
    crossDomain: true,

    xhrFields: {
        withCredentials: true
    },
});

firefox中的一切都很好。 Chrome未设置Cookie。仅当域字段被删除时,所有浏览器都在运行。我可以看到在下一个请求中(在设置cookie之后)cookie出现在标题中。响应设置cookie后的firefox请求示例(当响应有域字段时):

Cookie: Auth=l_hash=123456&user=xyz&remember_me=false

2 个答案:

答案 0 :(得分:6)

我认为问题可能出在localhost,这不是Set-Cookie标头的有效域。根据{{​​3}},它必须包含至少一个“嵌入”点。 FireFox可以以较少限制的方式实现此目的。请尝试使用您的IP地址。

答案 1 :(得分:5)

我遇到了同样的问题,结果发现浏览器设置阻止了第三方Cookie(Chrome>设置>高级设置>隐私>内容设置>阻止第三方Cookie和网站数据) 。允许cookie解决问题!