I want to open a json file on my JavaScript code, as this one:
[{
"x1": "5",
"y1": "5",
"x2": "7",
"y2": "6"
}, {
"x1": "5",
"y1": "4",
"x2": "8",
"y2": "8"
}]
Initially, I tried to open a local file, but it's not possible, right?
I tried this:
var xmlhttp = new XMLHttpRequest();
var dados = "file:///home/dan/Desktop/data.json";
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
myFunction(xmlhttp);
}
};
xmlhttp.open("GET", dados, true);
xmlhttp.send();
function myFunction(response) {
var arr = JSON.parse(response);
var i;
var out = "<table>"
for (i = 0; i < 2; i++) {
out += "<tr><td>" +
arr[i].x1 +
"</td><td>" +
arr[i].x1 +
"</td><td>" +
arr[i].x1 +
"</td></tr>";
}
out += "</table>";
}
(this create a table, just to test if it's working).
Or what should I do to open a Json file? Use XAMPP? How can I use XAMPP? Or there are another solution?
Thanks