JavaScript を使用して JSON ファイルを読み込もうとしています。関数を実行するHTMLにボタンを配置しましたloadAJAX
。しかし、11行目でエラーが発生し続けvar items = JSON.parse(request.responseText);
ますunexpected end of input
。何度か確認しましたが、これに対する解決策が見つかりません。テストサイト
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."
}
]
}