I have to read JSON file dynamically when I get "$ref" in Json schema.
I have tried accessing json file using node.js 'fs' module. But that gives error 'fs.readFile()' is not function.
I have later tried the Ajax call but I got following error.
XMLHttpRequest cannot load file:///Users/as6/Downloads/1099K.json. Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, https.
Below is my code to read json file in javascript.
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);
}
readTextFile("file:///Users/as6/Downloads/1099K.json", function(text){
var data = JSON.parse(text);
console.log(data);
});
How will I be able to read JSON file in javascript without Jquery?