使用 AJAX 遍历一组 MYSQL 数据的问题。我可以显示第一条记录,但总共有 7 条记录。我尝试了几种方法都没有成功。
AJAX 脚本:
<script type="text/javascript">
$(document).ready(function() {
$('#btn_get').on('click', function(e) {
$.ajax({
type: 'GET',
cache: false,
url: 'testData.php',
dataType: 'json',
success:function(info){
$('#getdiv').html("Page Viewed:" + info[0].page_ur + "<br />" + "Page count: " + info[0].page_count + "<br />");
console.log(info);
var len = info.length;
for(var i=0; i<len; i++)
{$('#getdiv2').html("<br />"+" Page Viewed:" + info[i].page_ur );}
}
});
e.preventDefault();
});
});
</script>
来自 console.log(info) 的结果;
控制台日志(信息)
[{page_ur:"test record page1", page_count:"1"}, {page_ur:"test record page2", page_count:"4"}, {page_ur:"test record page3", page_count:"3"}, {page_ur:"test record page5", page_count:"7"}, {page_ur:"test record page4", page_count:"9"}, {page_ur:"test record page6", page_count:"6"}, {page_ur:"test record page7", page_count:"2"}]