如何从Jquery ajax响应html收集收集数据?

时间:2015-06-02 10:16:08

标签: jquery ajax codeigniter

我使用Codeigniter和Jquery ajax从mysql调用数据并发送到html表单。

问题:我不知道如何从 res 对象调用数据。

这是我的控制者

public function select_post(){

        $data = $this->ads_m->get_post('ads',array('user_id'=>  $this->user->user_id()));
        IF($data){
            foreach($data as $row ){

                $response = array(
                    'name'=>$row->name,
                    'price'=>$row->price,
                    'addr'=>$row->des,
                    'img'=>$row->img,
                    'status'=>$row->status,
                );
                echo json_encode(array('res'=>$response));
            }
        }else{
            echo json_encode(array('res'=>FALSE));
        }
    }

这是模型功能

public function get_post($from, $where = False) {

        $this->db->select("*");
        $this->db->from($from);
        if ($where) {
            $this->db->where($where);
        }
        $q = $this->db->get();
        if($q->num_rows()>0){
            return $q->result();
        }
    }

这是Ajax

<script type="text/javascript">
       $(document).ready(function () {
        $.ajax({
         url: '<?php echo base_url('ads/select_post'); ?>',
         data: {},
         dataType: "json",
         cache: false,
         success: function (data) {
         console.log(data.['res']['name']);
       }
    });

});

我得到如下结果

{"res":{"name":"dfasdfas","price":"0","addr":"sadfdfasdfasdf","img":"","status":"1"}}{"res":{"name":"Heng"
,"price":"0","addr":"asdfasfasdfasdfasdfasdfasfasdfasdfasdfasdfasfasdfasdfasdfasdfasfasdfasdfasdfasd
fasfasdfasdfasdfasdfasfasdfasdfasdfasdfasfasdfasdfasdfasdfasfasdfasdfasdfasdfasfasdfasdfasdfasdfasfasdfasdfasdf"
,"img":"","status":"1"}}{"res":{"name":"asdDasdA","price":"0","addr":"ASDasdASD","img":"","status":"1"
}}{"res":{"name":"asdfas","price":"0","addr":"dfasdf","img":"","status":"1"}}

3 个答案:

答案 0 :(得分:1)

//首先更改响应 public function select_post(){

    $data = $this->ads_m->get_post('ads',array('user_id'=>  $this->user->user_id()));
    IF($data){
        var $responseArray = [];
        foreach($data as $row ){

            $response = array(
                'name'=>$row->name,
                'price'=>$row->price,
                'addr'=>$row->des,
                'img'=>$row->img,
                'status'=>$row->status,
            );
            array_push($responseArray, $response);

        }
        echo json_encode($responseArray);
    }else{
        echo json_encode(array('res'=>FALSE));
    }
}

//Then used below code to get the response in ajax
$(document).ready(function () {
    $.ajax({
        url: '<?php echo base_url('ads/select_post'); ?>',
        data: {},
        dataType: "json",
        cache: false,
       success: function (data) {
          $.each(data, function(item) {
          console.log(item.name);
       });
    }
});

答案 1 :(得分:0)

在你的控制器中,在for循环后执行json_encode

foreach($data as $row ){
    $response[] = array(
        'name'=>$row->name,
        'price'=>$row->price,
        'addr'=>$row->des,
        'img'=>$row->img,
        'status'=>$row->status,
    );
}
echo json_encode(array('res'=>$response));

然后在ajax中检查data.res是否返回数据 如果它正在返回数据,则在其上申请循环以分配“名称”

答案 2 :(得分:0)

使用此

public function select_post(){

        $data = $this->ads_m->get_post('ads',array('user_id'=>  $this->user->user_id()));
        if($data){
            foreach($data as $row ){

                $response = array(
                    'name'=>$row->name,
                    'price'=>$row->price,
                    'addr'=>$row->des,
                    'img'=>$row->img,
                    'status'=>$row->status,
                );
                echo json_encode(array('res'=>$response));
            }
        }else{
            echo json_encode(array('res'=>FALSE));
        }
    }