我们如何将js数据从js显示到html
这是我试过的代码
<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);
我做错了......
我认为这个 categories.date_ + i 有问题,我们怎样才能添加0,1,2,......
答案 0 :(得分:1)
我已经修改了你的代码,请尝试五次
正确的代码:
<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);