I need to load json from local filesystem. This is what I tried:
index.htlm:
<html>
<head>
<title>JSON File Locally by Javascript Without JQuery</title>
<script type="text/javascript" src="script.js"></script>
</head>
<body onload="loadJSON()">
Load JSON File Locally by Javascript Without JQuery
</body>
</html>
script.js:
function loadJSON(callback) {
var xobj = new XMLHttpRequest();
xobj.overrideMimeType("application/json");
xobj.open('GET', 'employee.json', true);
xobj.onreadystatechange = function () {
if (xobj.readyState == 4 && xobj.status == "200") {
callback(xobj.responseText);
}
}
xobj.send(null);
}
loadJSON(function(response) {
jsonresponse = JSON.parse(response);
console.log(jsonresponse[0].name);
});
employee.json:
[{"name" : "Harry", "age" : "32"}]
But nothing cant be read on console.log output. I tried with XMLHttpRequest()
function but soon realised it is only for web based json files. Any help is appreceated.