Skip to content

Instantly share code, notes, and snippets.

@kindziora
Forked from sindresorhus/simplexhr.js
Last active June 28, 2020 07:32
Show Gist options
  • Save kindziora/b4a877a790cac97bd584dae182752c03 to your computer and use it in GitHub Desktop.
Save kindziora/b4a877a790cac97bd584dae182752c03 to your computer and use it in GitHub Desktop.
Simpler XMLHttpRequest wrapper
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