Skip to content

Instantly share code, notes, and snippets.

@iakio
Forked from mewlist/jquery.cors.js
Created July 10, 2012 07:37
Show Gist options
  • Save iakio/3081837 to your computer and use it in GitHub Desktop.
Save iakio/3081837 to your computer and use it in GitHub Desktop.
Cross-Origin Resource Sharing (CORS) for jQuery for IE8+
$.ajaxTransport("+*", function( options, originalOptions, jqXHR ) {
if(jQuery.browser.msie && window.XDomainRequest) {
var xdr;
return {
send: function( headers, completeCallback ) {
// Use Microsoft XDR
xdr = new XDomainRequest();
xdr.open(options.type, options.url);
xdr.onload = function() {
if(this.contentType.match(/\/xml/)){
var dom = new ActiveXObject("Microsoft.XMLDOM");
dom.async = false;
dom.loadXML(this.responseText);
completeCallback(200, "success", [dom]);
}else{
completeCallback(200, "success", [this.responseText]);
}
};
xdr.ontimeout = function(){
completeCallback(408, "error", ["The request timed out."]);
};
xdr.onerror = function(){
completeCallback(404, "error", ["The requested resource could not be found."]);
};
xdr.send((options.hasContent && options.data) || null);
},
abort: function() {
if(xdr)xdr.abort();
}
};
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment