-
-
Save wlalele/5c2243fe34c2f67e7f46cc2d7499397a to your computer and use it in GitHub Desktop.
JSONHttpRequest
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
/** | |
* 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