doutong4088 2014-08-18 07:50
浏览 23
已采纳

如何从js到html显示或附加json数据

how can we display JSON data from js to html

here is the code i have tried

<script>
$.ajax({
    url:'load_categories.php',
    type:'get',
    data:{'from':loaded,'to':loadmore},
    success: function (res) {
        var categories = $.parseJSON(res);
      var i=0;
      for (var x in categories){
          alert(categories.date_+i);
           $('#categories').append('<div>'+(categories.date_+i+'</div>'); //not displaying on html
          i++;
      }
        $('#loadmore').attr('num_loaded',(loaded+10));
    }
});
</script>

<div id="categories"></div>

<?php
//load_categories.php
$res = mysql_query("SELECT * FROM impressions LIMIT $from,$to");
//echo "SELECT * FROM impressions LIMIT $from,$to";
$arr = array();
$i= 0;
while($row = mysql_fetch_array($res)) {

    $arr['rec_id_'.$i] = $row['rec_id'];
    $arr['date_'.$i]   = $row['date'];
    $i++;
}
echo json_encode($arr);

where im doing wrong...

i think something wrong with this categories.date_+i, how can we add 0,1,2,...

  • 写回答

1条回答 默认 最新

  • drnpwmq4536 2014-08-18 07:52
    关注

    I've fixed your code please five it a try

    The correct Code :

    <script>
    $.ajax({
        url:'load_categories.php',
        type:'get',
        data:{'from':loaded,'to':loadmore},
        success: function (res) {
            var categories = $.parseJSON(res);
    
        for(var i=0;i< categories.length;i++)
         { 
    
             var row = categories[i];
    
              alert(row.date);
             $('#categories').append('<div>'+row.date+'</div>'); //not displaying on html
    
          }
            $('#loadmore').attr('num_loaded',(loaded+10));
        }
    });
    </script>
    
    <div id="categories"></div>
    
    <?php
    //load_categories.php
    $res = mysql_query("SELECT * FROM impressions LIMIT $from,$to");
    //echo "SELECT * FROM impressions LIMIT $from,$to";
    $arr = array();
    while($row = mysql_fetch_array($res)) {
    
        $arr[] = row;
    }
    echo json_encode($arr);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 写uniapp时遇到的问题
  • ¥15 matlab有限元法求解梁带有若干弹簧质量系统的固有频率
  • ¥15 找一个网络防御专家,外包的
  • ¥100 能不能让两张不同的图片md5值一样,(有尝)
  • ¥15 informer代码训练自己的数据集,改参数怎么改
  • ¥15 请看一下,学校实验要求,我需要具体代码
  • ¥50 pc微信3.6.0.18不能登陆 有偿解决问题
  • ¥20 MATLAB绘制两隐函数曲面的交线
  • ¥15 求TYPCE母转母转接头24PIN线路板图
  • ¥100 国外网络搭建,有偿交流