我正在尝试使用DataTables实现this,并且在数据显示正确时,每当我尝试使用搜索栏,过滤器时,都会收到以下错误:
DataTables警告:table id = checkin - 无效的JSON响应。有关此错误的详细信息,请参阅http://datatables.net/tn/1
当我点击该链接并按照说明操作时,这是来自response.php的json:
{"draw":1,"recordsTotal":3,"recordsFiltered":3,"data":[["28","708089113","Nicole Foster","","","2016-04-15 14:27:36"],["29","708089113","Larry Quaglia","","","2016-04-15 14:28:38"],["30","708089113","Nicole Foster","Test","test@syr.edu","2016-04-21 09:18:59"]]}
我仍然是JSON和AJAX的新手,但当我检查该页面上的JSON Lint和JSON Parser链接输出的内容时,它表示它们是有效的。
这是我的代码:
Reports.php(只显示调用DataTables的脚本)
<script>
$( document ).ready(function() {
$('#checkin').DataTable({
"bProcessing": true,
"serverSide": true,
"dom": 'lBfrtip',
"ajax":{
url :"response.php", // json datasource
type: "post" // type of method ,GET/POST/DELETE
},
"buttons":[
{
extend: 'collection',
text: 'Export',
buttons: [
'copy',
'excel',
'csv',
'pdf',
'print'
]
}
]
});
});
</script>
Response.php
<?php
//include connection file
include_once("../connection.php");
// initilize all variable
$params = $columns = $totalRecords = $data = array();
$params = $_REQUEST;
//define index of column
$columns = array(
0 => 'id',
1 => 'suid',
2 => 'staffMember',
3 => 'studentName',
4 => 'studentEmail',
5 => 'checkinDateTime'
);
$where = $sqlTot = $sqlRec = "";
// check search value exist
if( !empty($params['search']['value']) ) {
$where .=" WHERE ";
$where .=" ( studentName LIKE '".$params['search']['value']."%' ";
$where .=" OR staffMember LIKE '".$params['search']['value']."%' ";
$where .=" OR studentEmail LIKE '".$params['search']['value']."%' ";
$where .=" OR suid LIKE '".$params['search']['value']."%' ";
$where .=" OR checkinDate LIKE '".$params['search']['value']."%' )";
}
// getting total number records without any search
$sql = "SELECT * FROM `checkin` ";
$sqlTot .= $sql;
$sqlRec .= $sql;
//concatenate search sql if value exist
if(isset($where) && $where != '') {
$sqlTot .= $where;
$sqlRec .= $where;
}
$sqlRec .= " ORDER BY ". $columns[$params['order'][0]['column']]." ".$params['order'][0]['dir']." LIMIT ".$params['start']." ,".$params['length']." ";
$queryTot = mysqli_query($VisitorManagement, $sqlTot) or die("database error:". mysqli_error($VisitorManagement));
$totalRecords = mysqli_num_rows($queryTot);
$queryRecords = mysqli_query($VisitorManagement, $sqlRec) or die("error to fetch check-in data");
//iterate on results row and create new index array of data
while( $row = mysqli_fetch_row($queryRecords) ) {
$data[] = $row;
}
$json_data = array(
"draw" => intval( $params['draw'] ),
"recordsTotal" => intval( $totalRecords ),
"recordsFiltered" => intval($totalRecords),
"data" => $data // total data array
);
echo json_encode($json_data); // send data as json format
?>
Connection.php
$hostname_VisitorManagement = "localhost";
$database_VisitorManagement = "visitor-management";
$username_VisitorManagement = "***";
$password_VisitorManagement = "***";
$VisitorManagement = mysqli_connect($hostname_VisitorManagement, $username_VisitorManagement, $password_VisitorManagement, $database_VisitorManagement);
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
date_default_timezone_set('America/New_York');
有什么我想念的吗?我在他们的论坛上四处看看,但我没有看到类似我的问题的解决方案,就像我的代码一样。
答案 0 :(得分:0)
尝试将选项serverSide
更改为false
。如果设置为true,则每次尝试过滤/排序/更改页面时,表都会查询ajax源。如果您打算先检索所有数据,然后在客户端工作,serverSide
选项应为false
更新:所以我猜测问题是数据表在进行ajax调用时没有发送任何参数,然后查询构建错误。
尝试将这些参数添加到ajax.data选项中:
"ajax":{
url :"response.php", // json datasource
type: "post", // type of method ,GET/POST/DELETE
data: {}//data to send
},