Created
February 2, 2012 19:26
-
-
Save rlemon/1725245 to your computer and use it in GitHub Desktop.
simple xhr object.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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