Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CORS cookie with domain field is setting only in Firefox using 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.

Is it possible to get it working in other browsers than firefox?

Some request Headers:

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/

Response Headers:

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=/

Code:

$.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
    },
});

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
like image 721
Mariusz Avatar asked Dec 19 '12 13:12

Mariusz


2 Answers

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.

like image 82
Stan Avatar answered Nov 02 '22 13:11

Stan


I had this same problem, and it turned out that the browser settings were blocking third-party cookies (Chrome > Settings > Advanced Settings > Privacy > Content Settings > Block third-party cookies and site data). Allowing the cookies resolved the problem!

like image 7
Andrew M. Andrews III Avatar answered Nov 02 '22 14:11

Andrew M. Andrews III