對於一個學校項目,我將電報消息實時寫入JSON文件。現在我想在JSON中添加新消息時更新我的HTML。我的HTML與JQuery/Knockout.js一起工作。做這個的最好方式是什麼 ?有沒有辦法從json讀取數據到html實時?
$(document).ready(function(){
var viewModel = function() {
console.log('test');
$.getJSON('/apps/telegram/getmessages', function(data) {
//console.log(data["messages"]);
var jsondata = data["messages"];
console.log(jsondata);
for(index = 0; index<jsondata.length; ++ index){
console.log(jsondata[index]);
var message = jsondata[index]["message"];
var firstname = jsondata[index]["first_name"];
console.log(message);
$('#messages').append('<p>' + message + '</p>');
}
})
.error(function(error){
console.log(error);
});
}
ko.applyBindings(viewModel);
})
嗨。請閱讀[問]和[主題](https://stackoverflow.com/help/on-topic),特別是有關家庭作業的部分,並使用[mcve] –
重新編寫代碼,該代碼看起來大致可以。什麼不工作? – ADyson
告訴我們什麼是不工作 –