无效的json响应数据表

时间:2019-07-02 12:42:56

标签: php json datatables

我无法使用php datatables插件查看任何数据。 这是样板样例:

array(3430) { [0]=> array(13) { [0]=> string(1) "1" [1]=> string(4)
"2016" [2]=> string(9) "33-V-0004" [3]=> string(33) "edward hines jr 
va hospital (151)" [4]=> string(1) "9" [5]=> NULL [6]=> string(2) "24"
[7]=> string(2) "45" [8]=> string(1) "0" [9]=> string(1) "0" 
[10]=> string(1) "0" [11]=> string(1) "4" [12]=> string(1) "0" } 
[1]=> array(13) { [0]=> string(4) "1154" [1]=> string(4) "2017" 
[2]=> string(9) "33-V-0004" [3]=> string(33) "edward hines jr va 
hospital (151)" [4]=> string(1) "0" [5]=> NULL [6]=> string(2) "11" 
[7]=> string(2) "29" [8]=> string(1) "0" [9]=> string(1) "0" 
[10]=> string(1) "0" [11]=> string(1) "4" [12]=> string(1) "0" }

尝试访问data.php时,不返回任何输出。这是我的示例php代码:

<?php
require 'login.php';
$connection = new mysqli($host, $user, $pword, $database, godsipaddress);
$query = "SELECT ar_id, ar_year, r.registration, r.registration_facility_name, ar_dog_bcdetotal, ar_cat_bcdetotal, ar_gp_bcdetotal, ar_hamster_bcdetotal, ar_rabbit_bcdetotal, ar_nhp_bcdetotal, ar_sheep_bcdetotal, ar_pig_bcdetotal, ar_ofa_bcdetotal FROM annual_report join registration r on annual_report.ar_registration = r.registration_id";
$result = $connection->query($query);
$data = [];
while($row = $result->fetch_assoc()) {
    $data[] = array($row['ar_id'], $row['ar_year'], $row['registration'], $row['registration_facility_name'], $row['ar_dog_bcdetotal'], $row['ar_cat_cbdetotal'], $row['ar_gp_bcdetotal'], $row['ar_hamster_bcdetotal'], $row['ar_rabbit_bcdetotal'], $row['ar_nhp_bcdetotal'], $row['ar_sheep_bcdetotal'], $row['ar_pig_bcdetotal'], $row['ar_ofa_bcdetotal']);
    }
echo json_encode($data);
echo var_dump($data); //Testing
$connection->close();
?>

jQuery:

<script>
$(document).ready( function () {
    //$('#table_id').DataTable();
    $('#table_id').DataTable({
  //order: [[0, 'DESC']],
  //columnDefs: [
  //{'orderable': false, 'targets': [1,2,3,6,8]}
  //],
  ajax: {
    url: "data.php",
    dataSrc: function ( server_data ) {
      return server_data;
     }
 }
});
} );
</script>

json输出的方式可能出错吗?

1 个答案:

答案 0 :(得分:1)

根据DataTables.net JSON数据源需要两个关键信息:

  • 其中表示表中数据行的数据数组 在对象中
  • 每列的数据点在行对象/数组中。

然后,我建议您使用php格式的数组。

<?php
require 'login.php';
$connection = new mysqli($host, $user, $pword, $database, godsipaddress);
$query      = "SELECT ar_id, ar_year, r.registration, r.registration_facility_name, ar_dog_bcdetotal, ar_cat_bcdetotal, ar_gp_bcdetotal, ar_hamster_bcdetotal, ar_rabbit_bcdetotal, ar_nhp_bcdetotal, ar_sheep_bcdetotal, ar_pig_bcdetotal, ar_ofa_bcdetotal FROM annual_report join registration r on annual_report.ar_registration = r.registration_id";
$result     = $connection->query($query);
$data       = array();
while ($row = $result->fetch_assoc()) {
    $data[] = array(
        "Row ID " => $row['ar_id'],
        "Year" => $row['ar_year'],
        "registration" => $row['registration'],
        "Reg Facility" => $row['registration_facility_name'],
        "ar_dog_bcdetotal" => $row['ar_dog_bcdetotal'],
        "ar_cat_cbdetotal" => $row['ar_cat_cbdetotal'],
        "ar_gp_bcdetotal" => $row['ar_gp_bcdetotal'],
        "ar_hamster_bcdetotal" => $row['ar_hamster_bcdetotal'],
        "ar_rabbit_bcdetotal" => $row['ar_rabbit_bcdetotal'],
        "ar_nhp_bcdetotal" => $row['ar_nhp_bcdetotal'],
        "ar_sheep_bcdetotal" => $row['ar_sheep_bcdetotal'],
        "ar_pig_bcdetotal" => $row['ar_pig_bcdetotal'],
        "ar_ofa_bcdetotal" => $row['ar_ofa_bcdetotal']
    );
}
echo json_encode($data);
// echo var_dump($data); //Testing
$connection->close();
?>

然后您的ajax设置与此类似:

<script type="text/javascript">
    $(document).ready(function() {
    $('#TableID').DataTable( {
        "ajax": "data.php",
        "columns": [
            { "data": "RowID" },
            { "data": "Year" },
            { "data": "registration" },
            { "data": "RegFacility" },
            { "data": "ar_dog_bcdetotal" },
            { "data": "ar_cat_cbdetotal" }
        ]
    } );
} );
</script>