Я пытаюсь загрузить файл JSON через javaScript. У меня есть кнопка в HTML, которая выполняет функцию loadAJAX
. Но я продолжаю получать ошибку в строке 11 var items = JSON.parse(request.responseText);
, которая говорит unexpected end of input
. Я несколько раз проверял и не мог найти решение. Test SITEНеожиданный конец ввода в JS
script.js
function loadAJAX() {
var request;
if(window.XMLHttpRequest){
request = new XMLHttpRequest();
} else {
request = new ActiveXObject("Microsoft.XMLHTTP");
}
request.open("GET", "data.json");
request.onreadystatechange = function(){
if((request.status === 200) && (request.readyState === 4)){
var items = JSON.parse(request.responseText);
var output = "<ul>";
for (var key in items){
output += "<li>" + items[key].colorName + "</li>";
}
output += "</ul>";
document.getElementById("update").innerHTML = output;
}
};
request.send();
}
data.json
{
"colorsArray":[{
"colorName":"red",
"hexValue":"#f00",
"info" : "My favorite color."
},
{
"colorName":"green",
"hexValue":"#0f0",
"info" : "Old color for old things, like food ew."
},
{
"colorName":"blue",
"hexValue":"#00f",
"info" : "Reminds me of bruised arm."
},
{
"colorName":"cyan",
"hexValue":"#0ff",
"info" : "Not an idea what color this is."
},
{
"colorName":"magenta",
"hexValue":"#f0f",
"info" : "Every girl talks about her color being this."
},
{
"colorName":"yellow",
"hexValue":"#ff0",
""info" : "My mom likes yellow."
},
{
"colorName":"black",
"hexValue":"#000",
"info" : "Well now look at this color the new white lol."
}
]
}
'data.json' пуст. – Blender
Вставить json. Или подтвердите это самостоятельно по адресу http://jsonlint.com/ –
@Blender Извините, исправлена эта проблема. – techAddict82