0
$('.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));
        }
    }
4

2 回答 2

2

添加dataType:'json'到 的选项$.ajax,并使用data成功函数中的参数作为任何其他数组对象来迭代它

$.each(data, function( .. ) { .. } ):
于 2012-09-16T22:45:09.980 回答
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的options中,使用success函数中的参数数据为:

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");        
}
于 2012-09-17T07:30:20.453 回答