对于一个学校项目,我将电报消息实时写入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
告诉我们什么是不工作 –