Why is this necessary?
How does it work?
xhr-xdr-adapter replaces the native XMLHttpRequest with a custom function having the same API, only if it's running in IE 8 or IE 9. When the open() method is called, the library figures out whether to create a native XMLHttpRequest object, or an XDomainRequest.
The goal is to provide as complete support as possible for cross domain XMLHttpRequest on a browser such as IE 8. For this reason, the extra step is taken to support async=false requests, as well as doing a complete check for whether XDomainRequest should be used. This includes checking for base href="..." before concluding whether a request is cross-origin.
Limitations and alternatives
- No authentication or cookies can be sent
- POST or GET only
- No custom headers can be sent
- text/plain contentType only
- only http<-->http or https<-->https
If you're required to support IE 8 or 9, and make CORS requests that are outside of those limitations, there are a few alternatives.
XDomain is a popular alternative that involves loading an iframe from the remote server, and using postMessage to communicate with that iframe, which does the asset loading (from its own domain.) Another alternative is to proxy the requests via your own server, assuming the bandwidth and processing costs are not an issue.
Simply include xhr-xdr-adapter.js before making cross domain requests with the popular libraries. The function is small enough to incorporate into your build process and minify together with the rest of your code. Alternatively, you could conditionally include it in the relevant versions of IE only:
If your are using a npm module packager:
require('xhr-xdr-adapter'); import 'xhr-xdr-adapter';
Contributions are welcome. If you find issues or features that we could support on IE 8 and are not currently supported, please submit a pull request.
xhr-xdr-adapter is provided under the MIT license.