Reputation: 8283
I have a jQuery ajax call across a subdomain that works correctly except it's not sending the cookie. Is there any way to accomplish this?
Upvotes: 3
Views: 34555
Reputation: 13332
This sounds like expected behavior to me. Cookies are per domain (and that includes subdomains). But I think you can force it with something like this:
$.ajax({
headers: {'Cookie' : document.cookie },
url: "sub.domain.com",
success: function(){ ...
This is totally untested so let me know if it works ;)
EDIT: There is an alternative solution available using:
xhrFields: {
withCredentials: true
}
Check here: How do I SET a Cookie (header) with XMLHttpRequest in JavaScript?.
Also, you can set the cookies in PHP so that they are valid across all your subdomains. Something like this:
ini_set('session.cookie_domain', '.example.com')
Note the '.' before the domain - that will set the cookie for example.com and all its subdomains.
You can set session.cookie_domain in your app using the above or set it in your php.ini.
The above is stolen from here.
Upvotes: 4
Reputation: 2405
Shouldn't this work if you use a CORS capable browser and set the withCredentials attribute?
See HTTP Cookies and Ajax requests over HTTPS
Upvotes: 0