I'm trying to set a browser cookie using a jQuery .ajax() call, and it's not working.
console.log("before cookies:" + document.cookie);
$.ajax({
dataType: "json",
contentType: "application/json",
url: url,
type: "GET",
processData: false
}).then(function (data) {
console.log("after cookies:" + document.cookie);
});
On the server side, the system adds a Set-Cookie to the response:
Access-Control-Allow-Headers Origin, X-Requested-With, Content-Type, Accept
Access-Control-Allow-Origin *
Content-Length 63
Content-Type application/json;charset=ISO-8859-1
Expires Thu, 01 Jan 1970 00:00:00 GMT
Set-Cookie myApiKey=testkey;Expires=Wed, 20-Aug-2014 18:11:57 GMT;Max-Age=31536000
The output is empty:
before cookies:
after cookies:
Here's the rub. The main page is at one subdomain, and the ajax call is to another (api.mydomain.com). But I think I've set the CORS headers on the server side correctly. It should work.
Firebug does report that the cookie is set, but somehow it's not visible to the outer page. What's the trick?