Created
April 25, 2016 07:21
-
-
Save MRezaSafari/a66f3891d334b6183f488098cb2fa536 to your computer and use it in GitHub Desktop.
From http://stackoverflow.com/questions/19706046/how-to-read-an-external-local-json-file-in-javascript
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 readTextFile(file, callback) { | |
var rawFile = new XMLHttpRequest(); | |
rawFile.overrideMimeType("application/json"); | |
rawFile.open("GET", file, true); | |
rawFile.onreadystatechange = function() { | |
if (rawFile.readyState === 4 && rawFile.status == "200") { | |
callback(rawFile.responseText); | |
} | |
} | |
rawFile.send(null); | |
} | |
//usage: | |
readTextFile("/Users/Documents/workspace/test.json", function(text){ | |
var data = JSON.parse(text); | |
console.log(data); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment