0

我正在尝试通过 javaScript 加载 JSON 文件。我在执行该功能的 HTML 中有放置按钮loadAJAX。但是我在第 11 行不断收到一个错误var items = JSON.parse(request.responseText);,上面写着unexpected end of input. 我已经检查了几次,但找不到解决方案。测试地点

脚本.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();
}

数据.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."
        }
    ]
}
4

1 回答 1

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

于 2013-06-22T02:28:11.417 回答