I am learning to read an external local JSON files into javascript. I follow the link below
How to read an external local JSON file in Javascript
However, I am not able to make it. I have a test.json in my C drive, which path is "c:\\test.json". I think my program works like this: when the content in the local json file is updated, my program is going to alert the data in the json file. I don't know what is wrong in my program. Hope someone could help me out. Thank you in advance.
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>noName</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://ajax.googleapis.com/ajax/libs/jqueryui/1.12.1/themes/smoothness/jquery-ui.css">
<script type="text/javascript" src="test.json"></script>
<script type="text/javascript" src="javascrip.js"></script>
</head>
<script type="text/javascript">
$(document).ready(function() {
alert("goes here ");
readTextFile("c:/test.json", function(text){
var data = JSON.parse(text);
alert(data);
});
});
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);
}
test.json
{"notes":["s0","s5","s4","s3","s2","s1"]}