Skip to content

Instantly share code, notes, and snippets.

@rlemon
Created February 2, 2012 19:26
Show Gist options
  • Save rlemon/1725245 to your computer and use it in GitHub Desktop.
Save rlemon/1725245 to your computer and use it in GitHub Desktop.
simple xhr object.
var xhr = {
xmlhttp: (function() {
var xmlhttp;
try {
xmlhttp = new XMLHttpRequest();
} catch (e) {
try {
xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
} catch (er) {
try {
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
} catch (err) {
xmlhttp = false;
}
}
}
return xmlhttp;
}()),
post: function(options) {
this.request.apply(this, ["POST", options]);
},
get: function(options) {
this.request.apply(this, ["GET", options]);
},
request: function(type, options) {
if (this.xmlhttp && options && 'url' in options) {
var _xhr = this.xmlhttp;
_xhr.open(type, options.url, true);
_xhr.onreadystatechange = function() {
if (_xhr.readyState == 4 && _xhr.status == 200) {
if( typeof options.success === 'function' ) {
options.success.apply(this, [_xhr.responseText]);
}
} else {
if( typeof options.failure === 'function' ) {
options.failure.apply(this);
}
}
};
_xhr.send(null);
}
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment