循環我有3個文件閱讀JSON數據,並通過它
- 的index.html
- data.json
- 我WAMP的服務器上app.js
。我希望遍歷JSON數據並更新HTML文件的內容。
這些都是我的文件的內容:
data.json
{
"user": [{
"goal": "HTML essential training",
"type": "Beginner",
"date": "20/07/2016"
}, {
"goal": "CSS essential training",
"type": "Beginner",
"date": "30/07/2016"
}, {
"goal": "CSS core concepts",
"type": "Intermediate",
"date": "10/08/2016"
}, {
"goal": "Javascript essential training",
"type": "Beginner",
"date": "20/08/2016"
}, {
"goal": "Object Oriented JS",
"type": "Advanced",
"date": "30/08/2016"
}]
}
app.js
var request = new XMLHttpRequest();
request.open('GET', 'data.json');
request.onreadystatechange = function() {
if ((request.readyState === 4) && (request.status === 200)) {
var items = JSON.parse(request.responseText);
console.log(items);
for (var key in items) {
console.log(key);
var output = "<tr><td><input type='checkbox' name='record'></td><td>" + items[key].goal + "</td><td>" + items[key].type + "</td><td>" + items[key].date + "</td></tr>";
console.log(output);
$("table tbody").append(output);
output = '';
}
}
}
request.send();
當我運行這段代碼,一行的所有值創建設置爲undefined。我認爲我的循環邏輯存在問題,請幫助我。
謝謝你幫助我! –
@RakeshVenki歡迎您:) –
「你一直用這個詞,我不認爲這意味着你的想法。」 [沒有像「JSON對象」](http://benalman.com/news/2010/03/theres-no-such-thing-as-a-json/)。 –