Skip to content

Instantly share code, notes, and snippets.

@HenriqueSilverio
Created April 10, 2014 18:50
Show Gist options
  • Save HenriqueSilverio/10411331 to your computer and use it in GitHub Desktop.
Save HenriqueSilverio/10411331 to your computer and use it in GitHub Desktop.
Module to get JSON data with XMLHttpRequest.
(function() {
'use strict';
var getJSON = function( url, callback ) {
var xhr = new XMLHttpRequest();
xhr.open( 'GET', url );
xhr.onreadystatechange = function() {
if( xhr.readyState === 4 && xhr.status === 200 ) {
var type = xhr.getResponseHeader( 'Content-Type' );
if( type === 'application/json' ) {
callback( JSON.parse( xhr.responseText ) );
}
}
};
xhr.send( null );
};
window.getJSON = getJSON;
}());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment