繁体   English   中英

数据表警告表 id=datatables-example - 无效的 json 响应

[英]Datatables warning table id=datatables-example - invalid json response

在这里,我是ajax DataTables新手。 我正在尝试connect我的database并将fetch recordsdataTable 但我收到了这个错误:

datatables 警告表 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> html

参考: https : //datatables.net/examples/data_sources/server_side

在上面的示例中, 仅提供 json 输出。

所以,尝试修复你的apt_table admin.php

试试这个小调试(在“post”末尾检索逗号)

"ajax" : {
           url: "sidebar/apt_table admin.php",
           method: "POST"
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM