-1
I am reading a JSON file that is located in a folder within the main project using Javascript. The code works in all browsers except Google Chrome. Error that appears in Chrome:
Code:
function readJsonFile(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);
}
function pintarMapa(estado, strokeColor, fillColor)
{
readJsonFile("file:///Estados/"+estado+".json", function(data) {
var dados = JSON.parse(data);
var coord = new google.maps.Polygon({
paths: dados.borders,
strokeColor: strokeColor,// cor da borda
strokeOpacity: 0.8,
strokeWeight: 2,
fillColor: fillColor,// cor da área
fillOpacity: 0.3
});
coord.setMap(map);
console.log(dados);
});
}
I don’t understand, so what changes in the code?
– Bruno Oliveira
I edited the answer, I hope it helps.
– iamdlm
The error continues...
– Bruno Oliveira
Does the project have a server part? You will need to have a local server running (apache in the case of linux, or IIS in the case of c#, for example). Only with Javascript you won’t be able to read the file, it’s a security measure of current browsers, only older versions of IE allow.
– iamdlm
So, even with all the . json files in my folder I won’t be able to read on google Chrome? The project isn’t on a server.
– Bruno Oliveira
Exactly, the security of Chrome is tighter but I believe that in the future all browsers will have the same behavior. An alternative (untested) is to host for example in Dropbox and use the generated link to access json.
– iamdlm
If my initial response helped to understand the problem and how it works when files are on the server, please mark as the correct answer
– iamdlm