Utknąłem, próbując uzyskać poprawną ścieżkę do pliku lokalnego. Mam następujące katalogi:Ścieżka pliku analizy pliku JSON
Resources ->
data ->
file.json
js ->
folder ->
script.js
html ->
folder ->
file1.html
jestem realizujących script.js
od file1.html
, z kodem JS:
var answers = JSON.parse('../../data/file.json');
alert(answers);
Ale to nie działa, nawet nie uruchamia się alarm. Co jest nie tak?
Również próbowałem to:
function readJSON(file) {
var request = new XMLHttpRequest();
request.open('GET', file, false);
request.send(null);
if (request.status == 200)
return request.responseText;
};
var temp = readJSON('../../data/file.json');
alert(temp);
Alert niezdefiniowane w niniejszym przypadku.
'Alert nieokreślony dla mnie." Jaki był "status", czy spowodował błąd w twojej konsoli? –
Wreszcie, 'if (request.status == 200)' spowodowało problemy dla mnie. Daje 0, nawet jeśli znajdzie plik. Po usunięciu wszystko jest w porządku. Dziękuje wszystkim! – jagger