Question

$.cookie is not reading cookies that have been previously set by the server response.

I can read cookies that have been set with $.cookie().

I can see all domain cookies set with Firefox's Web Developer add on.
Also, the server side can see the request cookies, so the browser definitely has them.

I have experimented with expiry times with no avail.

Was it helpful?

Solution

The problem is that the server side is sending the cookies with the HttpOnly setting as described here: http://en.wikipedia.org/w/index.php?title=HTTP_cookie#HttpOnly_cookie

Cookies sent this way are not accessible thru document.cookie. This is generally used to help protect the cookie value against possible XSS attacks on your site.

Edit: You didn't mention which technology you're using on the server side to set the cookies. In case you're using PHP, this link lists the possible ways the HttpOnly flag could be set:

https://www.owasp.org/index.php/HttpOnly#Using_PHP_to_set_HttpOnly

OTHER TIPS

UPDATE: I was wrong in my original answer (see below)... Pages further down the path tree can read upper level cookies, so the paths don't have to match exactly. Leaving my original answer in case someone else makes the same mistake. MY issue was caused by me setting a cookie from javascript, then trying to modify the cookie server side. Server targeted cookie with path "/", so cookie with path "/mypages" was not changed.

$.cookie can only read cookies under the current document path, so if your page is >mysite.com/mypages/mypage.aspx, the server will set the path as "/" when setting a cookie, >but $.cookie will try to read from path "/mypages" and fail to read the cookie. I just ran >into this problem myself. If you don't have differing paths like this, then this is likely >not your issue.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top