Skip to content

Instantly share code, notes, and snippets.

@sebmarkbage
Created February 14, 2010 19:15
Show Gist options
  • Save sebmarkbage/304197 to your computer and use it in GitHub Desktop.
Save sebmarkbage/304197 to your computer and use it in GitHub Desktop.
if (window.XDomainRequest) (function(){
var onLoad = function(){
cleanup(this.xhr);
this.response = {text: this.xhr.responseText, xml: this.xhr.responseXML};
this.success(this.response.text, this.response.xml);
};
var onError = function(){
cleanup(this.xhr);
this.response = {text: null, xml: null};
this.failure();
};
var cleanup = function(xhr){
if (this.xhr.onload) this.xhr.onload = this.xhr.onerror = this.xhr.ontimeout = function(){};
};
Class.refactor(Request, {
openRequest: function(method, url, data, xd){
if (xd && this.options.async){
this.xhr = new XDomainRequest();
this.xhr.open(method.toUpperCase(), url);
this.xhr.onload = onLoad.bind(this);
this.xhr.onerror = this.xhr.ontimeout = onError.bind(this);
this.fireEvent('request');
this.xhr.send(data);
return this;
}
return this.previous.apply(this, arguments);
},
getHeader: function(name){
return this.previous(name) || (name == 'Content-type' ? this.xhr.contentType || null : null);
},
cancel: function(){
cleanup(this.xhr);
return this.previous();
}
});
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment