After logging in via $.ajax()
to a site, I am trying to send a second $.ajax()
request to that site - but when I check the headers sent using FireBug, there is no session cookie being included in the request.
What am I doing wrong?
After logging in via $.ajax()
to a site, I am trying to send a second $.ajax()
request to that site - but when I check the headers sent using FireBug, there is no session cookie being included in the request.
What am I doing wrong?
Using
as part of my jQuery ajax call was only part of the solution. I also needed to have the headers returned in the OPTIONS response from my resource:
It was important that only one allowed "origin" was in the response header of the OPTIONS call and not "*". I achieved this by reading the origin from the request and populating it back into the response - probably circumventing the original reason for the restriction, but in my use case the security is not paramount.
I thought it worth explicitly mentioning the requirement for only one origin, as the W3C standard does allow for a space separated list -but Chrome doesn't! http://www.w3.org/TR/cors/#access-control-allow-origin-response-header NB the "in practice" bit.
You have to initialize the session before you trying to login.
For php, you have to do
on the page from where you start the login ajax call.
So that the
SESSIONID
will be created and stored the browser cookie. And sent along with request header during the ajax call, if you do the ajax request to the same domainFor the successive ajax calls browser will use the
SESSIONID
that created and stored initially in browser cookie, unless we clear the browser cookie or do logout (or set another cookie)If you are developing on
localhost
or a port on localhost such aslocalhost:8080
, in addition to the steps described in the answers above, you also need to ensure that you are not passing a domain value in the Set-Cookie header.You cannot set the domain to
localhost
in the Set-Cookie header - that's incorrect - just omit the domain.See Cookies on localhost with explicit domain and Why won't asp.net create cookies in localhost?
After trying out the other solutions and still not getting it to work, I found out what the problem was in my case. I changed contentType from "application/json" to "text/plain".
I was having this same problem and doing some checks my script was just simply not getting the sessionid cookie.
I figured out by looking at the sessionid cookie value in the browser that my framework (Django) was passing the sessionid cookie with HttpOnly as default. This meant that scripts did not have access to the sessionid value and therefore were not passing it along with requests. Kind of ridiculous that HttpOnly would be the default value when so many things use Ajax which would require access restriction.
To fix this I changed a setting (SESSION_COOKIE_HTTPONLY=False) but in other cases it may be a "HttpOnly" flag on the cookie path
Perhaps not 100% answering the question, but i stumbled onto this thread in the hope of solving a session problem when ajax-posting a fileupload from the assetmanager of the innovastudio editor. Eventually the solution was simple: they have a flash-uploader. Disabling that (setting
in asset.php) and the lights started blinking again.
As these problems can be very hard to debug i found that putting something like the following in the upload handler will set you (well, me in this case) on the right track:
A dive into the log and I quickly spotted the missing session, where no cookie was sent.