Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ajax/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
仅在使用jQuery AJAX的Firefox中设置带有域字段的CORS cookie_Jquery_Ajax_Cookies_Cross Domain - Fatal编程技术网

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

仅在使用jQuery AJAX的Firefox中设置带有域字段的CORS cookie,jquery,ajax,cookies,cross-domain,Jquery,Ajax,Cookies,Cross Domain,当使用跨站点请求添加域字段时,我无法设置cookie。我试图通过jQueryAjax调用请求来实现这一点 有没有可能让它在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:h

当使用跨站点请求添加
字段时,我无法设置cookie。我试图通过jQueryAjax调用请求来实现这一点

有没有可能让它在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
    },
});
响应标题:

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
    },
});
代码:

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请求示例(当响应具有域字段时):


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

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

您确定
localhost
Set Cookie
头的有效域吗?根据,它必须至少包含一个“嵌入”点。FireFox可能会以限制性较小的方式实现这一点。试试你的IP地址。@Stan非常感谢,请将其作为答案发布。只需将
localhost
替换为
127.0.0.1
就行了。谢谢是的,经过几个小时的挫折,这解决了我的问题!就我而言,Firefox阻止了第三方cookie,Chrome允许它们。