带有域字段的CORS cookie仅在使用jQuery AJAX的Firefox中设置 [英] CORS cookie with domain field is setting only in Firefox using jQuery AJAX

查看:231
本文介绍了带有域字段的CORS cookie仅在使用jQuery AJAX的Firefox中设置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用跨网站请求添加存档时,我无法设置Cookie。我试图通过调用请求通过jquery ajax。

I am not able to set cookie when domain filed is added using cross site request. I am trying to achieve that by calling request through jquery ajax.

是否可以使它在其他浏览器比firefox工作

Is it possible to get it working in other browsers than 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请求的示例:

Everything is fine in firefox. Chrome is not setting cookie. Only if domain field is removed all is working on every browser. I can see that in next request (after setting cookie) that cookie appears in header. Example from firefox request after response setting cookie (when response had domain field):

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


推荐答案

localhost ,它不是 Set-Cookie 头的有效域。根据 RFC ,它必须至少包含一个嵌入点。 FireFox可以以较少限制的方式实现这一点。请尝试使用您的IP地址。

I think the problem can be with localhost, which is not a valid domain for Set-Cookie header. According to RFC, it must contain at least one "embedded" dot. FireFox may implement this in a less restrictive way. Try your IP-address instead.

这篇关于带有域字段的CORS cookie仅在使用jQuery AJAX的Firefox中设置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆