Skip to content

Instantly share code, notes, and snippets.

@Raynos
Created February 2, 2012 17:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Raynos/1724574 to your computer and use it in GitHub Desktop.
Save Raynos/1724574 to your computer and use it in GitHub Desktop.
var xhr = (function() {
var XHR = function() {
var xmlhttp = false;
try {
xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
} catch (E) {
xmlhttp = false;
}
}
if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
try {
xmlhttp = new XMLHttpRequest();
} catch (e) {
xmlhttp = false;
}
}
this.xmlhttp = xmlhttp;
};
XHR.prototype.post = function(options) {
this.request.apply(this, ["POST", options]);
};
XHR.prototype.get = function(options) {
this.request.apply(this, ["GET", options]);
};
XHR.prototype.request = function(type, options) {
if (this.xmlhttp) {
var _xhr = this.xmlhttp;
_xhr.open(type, options.url, true);
_xhr.onreadystatechange = function() {
if (_xhr.readyState == 4) {
options.success.apply(this, [_xhr.responseText]);
}
// add failure callback
};
_xhr.send(null);
}
};
return new XHR();
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment