Skip to content

Instantly share code, notes, and snippets.

@wlalele
Forked from Zirak/jhr.js
Last active December 7, 2016 13:23
Show Gist options
  • Save wlalele/5c2243fe34c2f67e7f46cc2d7499397a to your computer and use it in GitHub Desktop.
Save wlalele/5c2243fe34c2f67e7f46cc2d7499397a to your computer and use it in GitHub Desktop.
JSONHttpRequest
/**
* Perform a JSON Http Request via XMLHttpRequest
* @param method
* @param url
* @param data
* @param callback
* @returns {XMLHttpRequest}
*/
var jhr = function ( method, url, data, callback ) {
var xhr = new XMLHttpRequest();
xhr.responseJSON = null;
xhr.open( method, url );
xhr.addEventListener( 'load', function () {
xhr.responseJSON = JSON.parse( xhr.responseText );
if (typeof callback === "function") {
callback( xhr.responseJSON, xhr );
}
});
xhr.send( JSON.stringify(data) );
return xhr;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment