Created
April 10, 2014 18:50
-
-
Save HenriqueSilverio/10411331 to your computer and use it in GitHub Desktop.
Module to get JSON data with XMLHttpRequest.
This file contains 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
(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