-
-
Save kindziora/b4a877a790cac97bd584dae182752c03 to your computer and use it in GitHub Desktop.
Simpler XMLHttpRequest wrapper
This file contains hidden or 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 simplerXHR = function( method, url) { | |
var _xhr = new XMLHttpRequest(); | |
_xhr.open( method, url ); | |
_xhr.setup = function(cb){ // hacky? maybe | |
cb(_xhr); | |
return _xhr; | |
}; | |
_xhr.done = function(cb){ // hacky? maybe | |
_xhr.onreadystatechange = function() { | |
if ( _xhr.readyState === 4 ) { | |
cb( _xhr.responseText ); | |
} | |
}; | |
return _xhr; | |
}; | |
return _xhr; | |
}; | |
/* | |
simplerXHR('get', 'http://google.com') | |
.done(function(data) { | |
console.log(data); | |
}).send(); | |
//OR///////////////////////////////////////// | |
simplerXHR('get', 'http://google.com') | |
.done(function(data) { | |
console.log(data); | |
}) | |
.setup(function (xhr) { | |
xhr.setRequestHeader("Content-Type", "application/json;charset=UTF-8"); | |
}) | |
.send(JSON.stringify({'sd' : 23})); | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment