我尝试从数据库表中检索数据并在DataTable中显示,但我收到的错误
DataTables警告:table id = example - 无效的JSON响应。有关此错误的详细信息,请参阅http://datatables.net/tn/1
您能否在这段代码中看到我的错误?
PHP
$displayBoxList = $this->category_m->getCategoryDisplayboxList(null, $type, $page);
$boxList = array();
foreach($displayBoxList as $key => $val)
{
$status = ($val['isActive'] == 1) ? 'Active' : 'De-active';
// $boxList[] = array($val['ID'],$val['heading'],$val['displayOrder'],$status);
$boxList[$key]['ID'] = $val['ID'];
$boxList[$key]['heading'] = $val['heading'];
$boxList[$key]['displayOrder'] = $val['displayOrder'];
$boxList[$key]['status'] = ($val['isActive'] == 1) ? 'Active' : 'De-active';
}
$json = json_encode($boxList);
echo $json;
AJAX
<script>
$(document).ready(function(){
var table = $('#example').DataTable({
"processing": true,
"serverSide": true,
"ajax": '<?=site_url()?>category/getDatatableData/<?=$activetab?>',
"createdRow": function(row, data, dataIndex){
$(row).attr('id', 'row-' + dataIndex);
}
});
table.rowReordering();
});
</script>
HTML
<table id="example" class="display" cellspacing="0" width="100%">
<thead>
<tr>
<th>ID</th>
<th>heading</th>
<th>displayOrder</th>
<th>status</th>
</tr>
</thead>
<tfoot>
<tr>
<th>ID</th>
<th>heading</th>
<th>displayOrder</th>
<th>status</th>
</tr>
</tfoot>
</table>
答案 0 :(得分:0)
它显示invalid JSON response
错误,因为您只将数据部分返回到datatable ajax。 Php文件应以下列格式返回数据:
$jsonData = array(
"draw" => intval($_REQUEST['draw']),
"recordsTotal" => 10, // you have to get this thorugh count(*) query
"recordsFiltered" => 8, // recordsFiltered will get count of records when search input is filled,
"data" => $boxList // This is your data in array
);
echo json_encode($jsonData);
当数据以echo
格式显示时,您将不会遇到invalid Json response
错误。 jQuery数据表接受json数组中draw
,recordsTotal
,recordsFiltered
,data
的json响应。