我在阅读JSON数据时遇到问题。
model.php
class chatApp extends database{
public $row = array();
public function IO(){
//SELECTING FROM DATABASE MAKE QUERY ....
$this->row = $mysql->show; //result
return $this->row; //return it
}}
Controller.php这样
if(isset($_GET['showmessage'])){
$chatApp = new chatApp(); // chat app object
$row = $chatApp->IO();
echo json_encode($row);} // echo it like json
view.php
function update(){
$.ajax({
type: "POST",
url: " http://localhost/chat/controller.php?showmessage",
dataType: 'json',
success: function(data) {
$("#chat").html(data); //The data ?
}
});
} setInterval (update, 1000);
所以问题是我无法在聊天div中显示数据?我做错了什么
答案 0 :(得分:2)
尝试:
$("#chat").html(jQuery.parseJSON(data));
或使用getJSON
代替ajax
:
function update(){
$.getJSON("http://localhost/chat/controller.php?showmessage",
function(data) {
$("#chat").html(data);
setTimeout(update,1000);
});
}
setTimeout(update,1000);
使用setTimeout
代替setInterval