Skip to content

Instantly share code, notes, and snippets.

@sindresorhus
Created January 9, 2012 15:19
Show Gist options
  • Star 19 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save sindresorhus/1583375 to your computer and use it in GitHub Desktop.
Save sindresorhus/1583375 to your computer and use it in GitHub Desktop.
Simple XMLHttpRequest wrapper
var xhr = function() {
var xhr = new XMLHttpRequest();
return function( method, url, callback ) {
xhr.onreadystatechange = function() {
if ( xhr.readyState === 4 ) {
callback( xhr.responseText );
}
};
xhr.open( method, url );
xhr.send();
};
}();
/*
xhr('get', 'http://google.com', function(data) {
console.log(data);
});
*/
@DarkPark
Copy link

nice
but as far as I can see there may be problems with setRequestHeader (single XMLHttpRequest object but need to set different headers for different calls)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment