$('.more').live("click",function() {
var id = $('.wallPosts:last').attr("relOne");
$.ajax({
type: "POST",
url: "<?=base_url()?>index.php/regUserDash/ajaxMore",
data: {id: id},
cache: false,
success: function(data) {
$("#morebox").append(data.idWallPosts);
}
});
});
使用上面的代码我将如何输出一堆json返回值?以下是我的json值:
{"idwallPosts":"803"}{"idwallPosts":"798"}{"idwallPosts":"797"}{"idwallPosts":"796"}{"idwallPosts":"793"}{"idwallPosts":"792"}{"idwallPosts":"789"}{"idwallPosts":"785"}{"idwallPosts":"780"}
下面是我的codeigniter代码:
public function ajaxMore() {
$id = $this->input->post('id');
$result = $this->db->query("SELECT * FROM wallposts WHERE idwallPosts < '$id' ORDER BY idwallPosts DESC LIMIT 9");
foreach($result->result() as $row) {
echo json_encode(array('idwallPosts' => $row->idwallPosts));
}
}
答案 0 :(得分:2)
将dataType:'json'
添加到$.ajax
的选项中,并使用success函数中的参数data
作为任何其他数组对象进行迭代
$.each(data, function( .. ) { .. } ):
答案 1 :(得分:1)
您应该将ajaxMore()
功能更改为:
public function ajaxMore() {
$id = $this->input->post('id');
$result = $this->db->query("SELECT * FROM wallposts WHERE idwallPosts < '$id' ORDER BY idwallPosts DESC LIMIT 9");
$idwallPosts = array();
foreach($result->result() as $row)
{
$idwallPosts[] = $row->idwallPosts));
}
print json_encode(array('posts'=>$idwallPosts));
exit;
}
在ajax调用中,将dataType:'json'
添加到$ .ajax的选项中,并使用success函数中的参数data:
success: function(data) {
var div_to_append = "<div>";
$.each(data.posts, function(i,post){
div_to_append += "<p>" + post + "</p>";
});
$(div_to_append).appendTo("#morebox");
}