Created
February 6, 2014 13:18
-
-
Save mazell/8843945 to your computer and use it in GitHub Desktop.
JS: LoadJson
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
| function loadJSON(callback) { | |
| var xobj = new XMLHttpRequest(); | |
| xobj.overrideMimeType("application/json"); | |
| xobj.open('GET', 'file.json', true); | |
| xobj.onreadystatechange = function () { | |
| if (xobj.readyState == 4 && xobj.status == "200") { | |
| // .open will NOT return a value but simply returns undefined in async mode so use a callback | |
| callback(xobj.responseText); | |
| } | |
| } | |
| xobj.send(null); | |
| } | |
| // Call to function with anonymous callback | |
| loadJSON(function(response) { | |
| // Do Something with the response e.g. | |
| //jsonresponse = JSON.parse(response); | |
| // Assuming json data is wrapped in square brackets as Drew suggests | |
| //console.log(jsonresponse[0].name); | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment