-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":"1 日 1 個のりんごでも医者いらず"},{"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":"また別の日にもう 1 ドル"},{"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":"診療所に Wi-Fi はありますか?"},{"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

あなたのコードをローカルでテストし、サンプル データベースを使用しました。すべて正常に動作しました。誰かがすでに述べたように: JSON データと混合される他の「エコー」がないことを確認してください。そうしないと、mysql クエリによって返されたデータである可能性があります。

于 2013-06-25T13:05:52.437 に答える
0

header('Content-Type: application/json')PHPファイルに追加してみてください。

于 2013-06-26T10:33:41.310 に答える