0

我正在尝试使用 codeigniter 实现 jqGrid。我设置了一切,它似乎是正确的,网格正在显示但没有加载任何信息。构造 Json 结构的控制器打印以下内容:

{"page":1,"total":1,"records":2,"rows":[{"id":"132","cell":["user1","user1@yahoo.com","1123","22767830","22767830","address"]},{"id":"12222","cell":["user2","user2@gmail.com","212222","8888888","888888","address2"]}]}

这意味着数据库正在被正确访问,并且 JSON 的创建没有问题。

但是网格没有显示这些信息,所以我打印了 JSON,然后网格显示没有任何信息。

下面是控制器的代码:

function loadData(){           

        $page = isset($_POST['page'])?$_POST['page']:1; 
        $limit = isset($_POST['rows'])?$_POST['rows']:10; 
        $sidx = isset($_POST['sidx'])?$_POST['sidx']:'name'; 
        $sord = isset($_POST['sord'])?$_POST['sord']:'';         
        $start = $limit*$page - $limit; 
        $start = ($start<0)?0:$start; 

        $where = ""; 
        $searchField = isset($_POST['searchField']) ? $_POST['searchField'] : false;
        $searchOper = isset($_POST['searchOper']) ? $_POST['searchOper']: false;
        $searchString = isset($_POST['searchString']) ? $_POST['searchString'] : false;

        /**************************/


        //array to translate the search type
        $ops = array(
                'eq'=>'=', //equal
                'ne'=>'<>',//not equal
                'lt'=>'<', //less than
                'le'=>'<=',//less than or equal
                'gt'=>'>', //greater than
                'ge'=>'>=',//greater than or equal
                'bw'=>'LIKE', //begins with
                'bn'=>'NOT LIKE', //doesn't begin with
                'in'=>'LIKE', //is in
                'ni'=>'NOT LIKE', //is not in
                'ew'=>'LIKE', //ends with
                'en'=>'NOT LIKE', //doesn't end with
                'cn'=>'LIKE', // contains
                'nc'=>'NOT LIKE'  //doesn't contain
        );
        function getWhereClause($col, $oper, $val){
                global $ops;
                if($oper == 'bw' || $oper == 'bn') $val .= '%';
                if($oper == 'ew' || $oper == 'en' ) $val = '%'.$val;
                if($oper == 'cn' || $oper == 'nc' || $oper == 'in' || $oper == 'ni') $val = '%'.$val.'%';
                return " WHERE $col {$ops[$oper]} '$val' ";
        }
        $where = ""; //if there is no search request sent by jqgrid, $where should be empty
        $searchField = isset($_POST['searchField']) ? $_POST['searchField'] : false;
        $searchOper = isset($_POST['searchOper']) ? $_POST['searchOper']: false;
        $searchString = isset($_POST['searchString']) ? $_POST['searchString'] : false;

/**************************/


        if(!$sidx) 
            $sidx =1;
        $count = $this->db->count_all_results('info'); 
        if( $count > 0 ) {
            $total_pages = ceil($count/$limit);    
        } else {
            $total_pages = 0;
        }

        if ($page > $total_pages) 
            $page=$total_pages;

        $query = $this->JqgridSample->getAllData($start,$limit,$sidx,$sord,$where); 
        $responce->page = $page;
        $responce->total = $total_pages;
        $responce->records = $count;
        $i=0;
        foreach($query as $row) {
            $responce->rows[$i]['id']=$row->id;
            $responce->rows[$i]['cell']=array($row->name,$row->email,$row->passport,$row->phone,$row->fax,$row->address);
            $i++;
        }

        //return json_encode($responce);
       echo json_encode($responce);
    }

这是模型的代码:

function getAllData($start,$limit,$sidx,$sord,$where){

   $this->db->select('id,name,email,passport,phone,fax,address');
    $this->db->limit($limit);
     if($where != NULL)
          $this->db->where($where,NULL,FALSE);
       $this->db->order_by($sidx,$sord);
       $query = $this->db->get('info',$limit,$start);
       return $query->result();
}

这是视图的代码:

<body>
 <center>
    <h1>Codeigniter With JQGrid</h1>
<table id="list"></table><!--Grid table-->
<div id="pager"></div>  <!--pagination div-->
</center>
</body>

<script type="text/javascript">
    $(document).ready(function (){
        jQuery("#list").jqGrid({
            url: 'http://localhost/ProyectoNetbeans/CodeIgniter_2.1.3/index.php/Nacionalidades_controller/loadData',
            mtype : "post",             //Ajax request type. It also could be GET
            datatype: "json",            //supported formats XML, JSON or Arrray
            colNames:['Name','Email','Passport','Phone','Fax','Address'],       //Grid column headings
            colModel:[
                {name:'name',index:'name', width:100, align:"left"},
                {name:'email',index:'email', width:150, align:"left"},
                {name:'passport',index:'passport', width:100, align:"right"},
                {name:'phone',index:'phone', width:100, align:"right"},
                {name:'fax',index:'fax', width:100, align:"right"},
                {name:'address',index:'address', width:100, align:"right"}
            ],
            rowNum:10,
            width: 750,
            //height: 300,
            rowList:[10,20,30],
            pager: '#pager',
            sortname: 'id',
            viewrecords: true,
            rownumbers: true,
            gridview: true,
            caption:"List Of Person"
        }).navGrid('#pager',{edit:false,add:false,del:false});
    });
</script>

有人知道如何解决这个问题吗?

4

1 回答 1

0

在我看来,您应该设置Content-Type服务器响应的标头,而不仅仅是将 JSON 数据放在响应的正文中。一个可以使用

$this->output->set_header('Content-Type: application/json; charset=utf-8');

例如这样做。可能更好的事件是使用

$this->output->set_content_type('application/json')
             ->set_output(json_encode($re‌​sponce));

我不是 CodeIgniter 开发人员,但CodeIgniter文档中的外观似乎是您问题的原因。

于 2013-04-07T18:12:10.147 回答