我正在尝试通过javaScript加载JSON文件。我在执行函数loadAJAX
的HTML中放置了按钮。但是我在var items = JSON.parse(request.responseText);
的第11行unexpected end of input
上一直收到错误。我已经多次检查过,无法找到解决方法。 Test SITE
的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."
}
]
}
答案 0 :(得分:2)
"info: Old color for old things, like food ew."
你在这里不小心引用了整条线。对象属性的名称和值需要单独引用。重写为
"info": "Old color for old things, like food ew."
这也会进一步掩盖重复的引号字符:
""info" : "My mom likes yellow."
键名info
前面应该只有一个。