I am having a hard time finding an example how to read a simple .json file and I can read and create a JSON object. Here is my file named AllWeek.json
{
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": [101.46260261535645, 3.0185554329110373]
},
"properties": {
"_id": "AVSsYt-dJhbPevXZmtga",
"area": "candar kuteri plang",
"square_feet": 326
}
}
]
}
I have tried this code but it gives me CORS error.
readTextFile("AllWeek.json", function(text) {
var data = JSON.parse(text);
console.log(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);
}