简体   繁体   English

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

[英]how to display or append json data from js to html

how can we display JSON data from js to html 我们如何显示从js到html的JSON数据

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,... 我认为这个category.date_ + i有问题 ,我们如何添加0,1,2,...

I've fixed your code please five it a try 我已修复您的代码,请尝试5次

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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM