Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is the browser not setting cookies after an AJAX request returns?

Tags:

ajax

cookies

I am making an ajax request using $.ajax. The response has the Set-Cookie header set (I've verified this in the Chrome dev tools). However, the browser does not set the cookie after receiving the response! When I navigate to another page within my domain, the cookie is not sent. (Note: I'm not doing any cross-domain ajax requests; the request is in the same domain as the document.)

What am I missing?

EDIT: Here is the code for my ajax request:

$.post('/user/login', JSON.stringify(data)); 

Here is the request, as shown by the Chrome dev tools:

Request URL:https://192.168.1.154:3000/user/login Request Method:POST Status Code:200 OK  Request Headers: Accept:*/* Accept-Encoding:gzip,deflate,sdch Accept-Language:en-US,en;q=0.8 Connection:keep-alive Content-Length:35 Content-Type:application/x-www-form-urlencoded; charset=UTF-8 DNT:1 Host:192.168.1.154:3000 Origin:https://192.168.1.154:3000 Referer:https://192.168.1.154:3000/ User-Agent:Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/33.0.1750.154 Safari/537.36 X-Requested-With:XMLHttpRequest  Form Data: {"UserId":"blah","Password":"blah"}: 

Response:

Response Headers: Content-Length:15 Content-Type:application/json; charset=UTF-8 Date:Sun, 16 Mar 2014 03:25:24 GMT Set-Cookie:SessionId=MTM5NDk0MDMyNHxEdi1CQkFFQ180SUFBUkFCRUFBQVRfLUNBQUVHYzNSeWFXNW5EQXNBQ1ZObGMzTnBiMjVKWkFaemRISnBibWNNTGdBc1ZFcDNlU3RKVFdKSGIzQlNXRkkwVjJGNFJ6TlRVSHA0U0ZJd01XRktjMDF1Y1c1b2FGWXJORzV4V1QwPXwWf1tz-2Fy_Y4I6fypCzkMJyYxhgM3LjVHGAlKyrilRg==; HttpOnly 
like image 868
Matt Fichman Avatar asked Mar 16 '14 03:03

Matt Fichman


People also ask

Why are my browser cookies not setting?

Check out the OPTIONS response header ACCESS-CONTROL-ALLOW-CREDENTIAL whether it is set to true . If the server doesn't allow credentials being sent along, the browser will just not attach cookies and authorization headers.

Can AJAX response set cookie?

Yes, you can set cookie in the AJAX request in the server-side code just as you'd do for a normal request since the server cannot differentiate between a normal request or an AJAX request.

Does AJAX request send cookies?

Basically, ajax request as well as synchronous request sends your document cookies automatically.

What happens when JavaScript makes an AJAX request in a browser?

When you make an AJAX request, your browser sends an HTTP request to a given address. The server on the other end of the request responds, and returns the data to your browser. This is the same thing that happens when you navigate to a new web page.


1 Answers

OK, so I finally figured out the problem. It turns out that setting the Path option is important when sending cookies in an AJAX request. If you set Path=/, e.g.:

Set-Cookie:SessionId=foo; Path=/; HttpOnly 

...then the browser will set the cookie when you navigate to a different page. Without setting Path, the browser uses the "default" path. Apparently, the default path for a cookie set by an AJAX request is different from the default path used when you navigate to a page directly. I'm using Go/Martini, so on the server-side I do this:

session.Options(session.Options{HttpOnly: true, Path:"/"}) 

I'd guess that Python/Ruby/etc. have a similar mechanism for setting Path.

See also: cookies problem in PHP and AJAX

like image 127
Matt Fichman Avatar answered Sep 29 '22 09:09

Matt Fichman