php - 数据表警告表 id=datatables-example - 无效的 json 响应
问题描述
在这里我是新手ajax DataTables
。我正在尝试connect
我的database
和. 但我得到了这个错误:fetch records
dataTable
datatables warning table id=datatables-example - invalid json response有关此错误的更多信息,请参阅 http://datatables.net/tn/1
我遵循了名为 的教程web lesson
,以防我试图在互联网上找到解决方案但没有帮助我解决我的问题。这是我的HTML
片段:
<table id="datatables-example" class="table table-striped table-bordered" width="100%" cellspacing="0">
<thead>
<tr>
<th>Appointment ID</th>
<th>Doctor Name</th>
<th>Specialization</th>
<th>Patient Name</th>
<th>Fees</th>
<th>Appointment Date</th>
<th>Appointment Time</th>
<th>Status</th>
<th class="text-right">Action</th>
</tr>
</thead>
</table>
以下是Ajax
片段:
<script>
$(document).ready(function() {
load_data();
function load_data(){
var datatable= $('#datatables-example').DataTable({
"processing": true,
"serverSide": true,
"order" :[],
"ajax" : {
url: "sidebar/apt_table admin.php", // Url to which the request is send
method: "POST", // Type of request to be send, called as method
}
});
}
});
</script>
该apt_table admin.php
页面应为:
<?php
session_start();
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
try {
$conn = new mysqli("localhost", "root", "", "hmsproject");
$conn->set_charset("utf8mb4");
} catch(Exception $e) {
error_log($e->getMessage());
exit('Error connecting to database'); //Should be a message a typical user could understand
}
$columns= array('apt_id','doctor_name','specilization','patient_name','fees','apt_date','apt_time','admin_status');
$query="SELECT * FROM appointment as a,users as u WHERE a.user_id= u.user_id ORDER BY apt_id DESC";
if(isset($_POST["search"]["value"])){
$query .= '
WHERE apt_id LIKE "%'.$_POST["search"]["value"].'%"
OR doctor_name LIKE "%'.$_POST["search"]["value"].'%"
OR specilization LIKE "%'.$_POST["search"]["value"].'%"
OR patient_name LIKE "%'.$_POST["search"]["value"].'%"
OR fees LIKE "%'.$_POST["search"]["value"].'%"
OR apt_date LIKE "%'.$_POST["search"]["value"].'%"
OR apt_time LIKE "%'.$_POST["search"]["value"].'%"
OR admin_status LIKE "%'.$_POST["search"]["value"].'%"
';
}
if (isset($_POST["order"])) {
$query .= ' ORDER BY '.$columns[$_POST['order']['0']['column']].' '.$_POST['order']['0']['dir'].' ';
}else{
$query .= ' ORDER BY apt_id DESC';
}
$query1='';
if ($_POST["length"] != -1) {
$query1 = 'LIMIT '.$_POST['start'] .' , '.$_POST['length'];
}
$number_filter_row= mysqli_num_rows(mysqli_query($conn,$query));
$result =mysqli_query($conn,$query.$query1);
$data=array();
while($row = mysqli_fetch_array($result))
{
$sub_array =array();
$sub_array[] = '<td> '. $row["apt_id"].' </td>';
$sub_array[] ='<td> '. $row["doctor_name"].' </td>';
$sub_array[] ='<td> '. $row["specilization"].' </td>';
$sub_array[] ='<td> '. $row["patient_name"].' </td>';
$sub_array[] ='<td> '. $row["fees"].' </td>';
$sub_array[] ='<td> '. $row["apt_date"].' </td>';
$sub_array[] ='<td> '. $row["apt_time"].' </td>';
if($row["admin_status"]=="0") {
$sub_array[] ='<td> <span class="custom-badge status-red">Cancel</span>';
} else if($row["admin_status"]=="1") {
$sub_array[] ='<td> <span class="custom-badge status-green">Active</span>';
} else {
$sub_array[] ='<td> <span class="custom-badge status-blue">Pending</span>';
}
$sub_array[] = '<td class="text-right">
<div class="dropdown dropdown-action">
<a href="#" class="action-icon dropdown-toggle" data-toggle="dropdown" aria-expanded="false"><i class="fa fa-ellipsis-v"></i></a>
<div class="dropdown-menu dropdown-menu-right">
<!-- <a class="dropdown-item" href="edit-appointment.php"><i class="fa fa-pencil m-r-5"></i> Edit</a> -->
<a class="dropdown-item" href="" data-toggle="modal" id="rep1" data_id= '.$row['apt_id'] .' data-target="#active_appointment"><i class="fa fa-trash-o m-r-5"></i> Active</a>
<a class="dropdown-item" href="" data-toggle="modal" id="rep2" data_id='. $row['apt_id'].' data-target="#delete_appointment"><i class="fa fa-trash-o m-r-5"></i> Delete</a>
</div>
</div>
</td>';
$data[]=$sub_array;
}
function get($conn)
{
$query="SELECT * FROM appointment as a,users as u WHERE a.user_id= u.user_id ORDER BY apt_id DESC";
$result =mysqli_query($conn,$query);
return mysqli_num_rows($result);
}
$output= array(
"draw" => intval($_POST['draw']),
"recordsTotal" => get($conn),
"recordsFiltered" => $number_filter_row,
"data" => $data
);
echo json_encode($output);
?>
我不知道我哪里错了。请帮我。提前致谢。
解决方案
要实现服务器端 ajax 数据表,您的脚本应该返回 json 只有您发送带有一些<tr>
和<td>
参考:https ://datatables.net/examples/data_sources/server_side
在上面的示例源中,仅提供 json 输出。
所以,试着修复你的apt_table admin.php
推荐阅读
- vba - VBA 宏观区域问题
- python - 使用令牌从命令行授权
- php - 将 PHP 数组插入 MySQL,表作为变量或动态表
- amazon-web-services - 限制来自同一 AWS 账户中另一个 IAM 用户的一个 IAM 用户资源
- kotlin - 自定义 'typesafe' Int 类型
- jquery - Vue.js 无法获取单选按钮的新值
- kubernetes - Kubernetes 中的 PersistentVolumeClaim 未知
- docker - docker-compose 文件和 docker 文件有什么区别?
- javascript - 如何使用 javascript Lodash 将所有对象推入另一个数组
- python - Binlog 有事件但没有行?