-2

在这个上挠头。我正在尝试将 json 转换为 HTML 列表。如果 json 在静态文件中,则一切正常。如果我运行脚本,它不会。如果我在静态文件中使用脚本的输出,它就可以工作。

处理json的代码:

<div id="placeholder"></div>
    <script src="http://code.jquery.com/jquery-1.7.1.min.js"></script>
    <script>
         $.getJSON('community.php', function(data1) {
               var output="<ul>";
               for (var i in data1.users1) {
                 output+="<li>" + data1.users1[i].Community_post_author + " " + data1.users1[i].Community_post_message + "--" + data1.users1[i].Community_post_time_stamp+"</li>";
               }
               output+="</ul>";
               document.getElementById("placeholder").innerHTML=output;
         });
     </script>

创建json的代码:

$rows = array();
while($r = mysql_fetch_assoc($community_records)) {
  $rows[] = $r;
}

echo json_encode(array('users1' => $rows));

在此先感谢您的帮助!


根据评论(谢谢!)这里是脚本输出:

{"users1":[{"Community_post_time_stamp":"2013-06-25 06:41:13","Community_post_type":"community","Community_post_status":"live","Community_post_author":"MikeB","Community_post_message ":"系统检查"},{"Community_post_time_stamp":"2013-06-22 08:27:34","Community_post_type":"community","Community_post_status":"live","Community_post_author":"fish2011", "Community_post_message":"一天一个苹果,医生远离我"},{"Community_post_time_stamp":"2013-06-22 08:26:40","Community_post_type":"community","Community_post_status":"live ","Community_post_author":"grumpy","Community_post_message":"又是一天又一美元"},{"Community_post_time_stamp":"2013-06-22 08:26:10","Community_post_type":"community","Community_post_status":"live"," Community_post_author":"devgirl","Community_post_message":"赞这个节目"},{"Community_post_time_stamp":"2013-06-22 08:25:51","Community_post_type":"community","Community_post_status":" live","Community_post_author":"devguy","Community_post_message":"我今天清醒 1 年"},{"Community_post_time_stamp":"2013-06-22 08:25:28","Community_post_type":"community ","Community_post_status":"live","Community_post_author":"raccoongrrll","Community_post_message":"手机充电器丢了"},{"Community_post_time_stamp":"2013-06-22 08:24:49","Community_post_type":"community"," Community_post_status":"live","Community_post_author":"raccoongrrll","Community_post_message":"诊所有wifi吗?"},{"Community_post_time_stamp":"2013-06-22 08:23:41","Community_post_type ":"community","Community_post_status":"live","Community_post_author":"devguy","Community_post_message":"我女朋友快把我逼疯了"},{"Community_post_time_stamp":"2013-06-22 08:22 :36","Community_post_type":"community","Community_post_status":"live","Community_post_author":"devgirl","Community_post_message":"今天是我的生日!!!"}]}

4

2 回答 2

0

尝试添加header('Content-Type: application/json')到 PHP 文件。

于 2013-06-26T10:33:41.310 回答
0

I have tested your code locally and just used a sample database - everything worked fine. As someone already mentioned: Ensure that there is no other 'echo' that gets mixed with your JSON data, and otherwise it might be the data returned by the mysql query.. maybe it's empty?

于 2013-06-25T13:05:52.437 回答