Suppose I control two domains, www.api_domain.com
and www.website_domain.com
. www.api_domain.com
offers an API that requires the user to authenticate and then uses a session cookie to recognise the user who is making requests. www.website_domain.com
loads a script onto its pages from www.api_domain.com
and that script wants to make calls to API URLs on www.api_domain.com
with the current user's cookie and use the results in some way on the page from www.website_domain.com
.
For initially loading the script, or for any API URLs that don't require the user's session cookie to work, the easiest solution is simply to use an
Access-Control-Allow-Origin: http://www.website_domain.com
header on the response from www.api_domain.com
. This seems to work out of the box on all browsers besides IE, and although IE won't respect the Allow-Origin header on AJAX requests made using jQuery's AJAX methods, there are libraries like xdr.js which do some magic behind the scenes to make jQuery, IE and the Allow-Origin header play nice together and behave like in all other browsers (I don't know the details of what xdr.js does, but it works perfectly for non-credentialed requests as far as I can see).
The problem comes when I want to hit a URL on http://www.api_domain.com
that requires the user's session cookie. When this problem is discussed in a browser-agnostic setting, two solutions are usually proposed:
- Use
Access-Control-Allow-Credentials: true
on the response from to make cookies be sent even with cross-domain requests. - Create an iframe on the page on
http://www.website_domain.com
with originhttp://www.api_domain.com
, have the two windows communicate with each other using HTML5 post messages and delegate all responsibility for making requests tohttp://www.api_domain.com
to the iframe.
I would greatly prefer to use option 1 if possible, since it lets you write your Javascript code to use the API on http://www.api_domain.com
in the same way that you would write it to touch a same-domain API. To use the iframe approach, we'd need to learn or create some framework for sending AJAX-like requests to the iframe, with success and error handlers. It also means we need to create the code to be loaded into the iframe, which will just be a whole chunk of thin wrappers for hitting the API URLs. It just seems uglier, trickier, and harder to understand than the first approach.
However, I can't figure out how to make option 1 work on IE. I'm setting Access-Control-Allow-Credentials: true
on my API URLs, and all other browsers send cookies to those URLs, but IE 9 doesn't, even with the xdr.js library. (I haven't tested on IE 8.) There are no other symptoms to report whatsoever. I can see the proper Access-Control-Allow-Origin
and Access-Control-Allow-Credentials
headers in the responses from www.api_domain.com
when I view them in IE's developer tools, but there are no cookie headers in the request.
Is there some hack or magical incantation that I can use to make Internet Explorer respect the Access-Control-Allow-Credentials
header, or some other header I can use that IE recognises?