【问题标题】:Problems reading json data with jquery (chat application)使用 jquery 读取 json 数据的问题(聊天应用程序)
【发布时间】:2012-07-07 14:51:34
【问题描述】:

我在读取 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 中显示数据?我做错了什么

【问题讨论】:

  • 当您在浏览器中访问controller.php 时会发生什么?您是否看到任何 JSON 输出?
  • 你是如何在你的 javascript 中调用 update() 函数的?
  • @TomWalters 是的,我看到了 JSON:[{"id":"1","message":"sdvsvdsdv","nickname":""}]
  • @Austin setInterval(更新,1000);

标签: jquery ajax json chat


【解决方案1】:

尝试:

$("#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

【讨论】:

  • 应该没关系,因为他的数据类型设置为 json
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-09-20
  • 2015-06-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-02-25
相关资源
最近更新 更多