I am trying to read a local .JSON file and use JSON.parse to put it into a Javascript array. Any other piece of example code would also help. I am unable to do it using the following code, its not able to load a local file.
var xmlhttp = new XMLHttpRequest();
//xmlhttp.overrideMimeType("application/json"); //this line also didnt help
var url = "sample.json";
xmlhttp.open("GET", url, true);
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
testme(xmlhttp.responseText);
}
};
xmlhttp.send();
function testme(response){
var record = JSON.parse(response);
var out = "<table>";
for(var i = 0; i < record.length; i++) { //prints all the data to html
out += "<tr><td>" +
record[i].Name +
"</td><td>" +
record[i].City +
"</td><td>" +
record[i].Country +
"</td></tr>";
}
out += "</table>";
document.getElementById("dis").innerHTML = out;
}
the following errors occur
XMLHttpRequest cannot load file:///C:/Practice/CMPE%20273%20refresher/json/Sample.json. Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, https, chrome-extension-resource.transmit1 @ JSON.js:36transmit @ JSON.js:41onclick @ jsonweb.html:11
JSON.js:36 Uncaught NetworkError: Failed to execute 'send' on 'XMLHttpRequest': Failed to load 'file:///C:/Practice/CMPE%20273%20refresher/json/Sample.json'.