php datatable:无效的json响应

时间:2018-06-06 09:49:40

标签: php ajax datatable

请有人帮我解决我的代码有什么问题我得到的消息是" DataTables警告:table id = users - 无效的JSON响应。 "我很困惑我遵循数据表API说明,但为什么我没有得到结果?请提出任何建议或在我的代码上出错?

克林特边码

    <table cellpadding="1" cellspacing="1" id="users" class="display" width="100%">
    <thead>
    <tr>
        <th>ID</th>
        <th>simno</th>
        <th>city</th>
        <th>newno</th>
        <th>time</th>

    </tr>
    </thead>
    <tfoot>
    <tr>
       <th>ID</th>
        <th>simno</th>
        <th>city</th>
        <th>newno</th>
        <th>time</th>

    </tr>
    </tfoot>
</table>

<script type="text/javascript">
    $(document).ready(function () {
        $('#users').DataTable({
            "columns": [
                {"data": "id"},
                {"data": "simno"},
                {"data": "city"},
                {"data": "newno"},
                {"data": "time"}

            ],
            "processing": true,
            "serverSide": true,
            "ajax": {
                url: 'demo2.php',
                type: 'POST',
                dataType: "json",
    contentType: "application/json; charset=utf-8"
            }
        });
    });
</script>

也是它的服务器端代码

    <?php
ini_set('memory_limit', '8G');
ini_set('max_execution_time', 3000000);
?>
<?php

if (!empty($_POST) ) {



    define("HOST", "localhost");
    define("USER", "root");
    define("PASSWORD", "root");
    define("DB", "archive");
    define("MyTable", "bond");

    $connection = mysqli_connect(HOST, USER, PASSWORD, DB) OR DIE("Impossible to access to DB : " . mysqli_connect_error());


    function getData($sql){
        global $connection ;//we use connection already opened
        $query = mysqli_query($connection, $sql) OR DIE ("Can't get Data from DB , check your SQL Query " );
        $data = array();
        foreach ($query as $row ) {
            $data[] = $row ;
        }
        return $data;
    }

    /* Useful $_POST Variables coming from the plugin */
    $draw = $_POST["draw"];//counter used by DataTables to ensure that the Ajax returns from server-side processing requests are drawn in sequence by DataTables
    $orderByColumnIndex  = $_POST['order'][0]['column'];// index of the sorting column (0 index based - i.e. 0 is the first record)
    $orderBy = $_POST['columns'][$orderByColumnIndex]['data'];//Get name of the sorting column from its index
    $orderType = $_POST['order'][0]['dir']; // ASC or DESC
    $start  = $_POST["start"];//Paging first record indicator.
    $length = $_POST['length'];//Number of records that the table can display in the current draw
    /* END of POST variables */

    $recordsTotal = count(getData("SELECT * FROM ".MyTable));

    /* SEARCH CASE : Filtered data */
    if(!empty($_POST['search']['value'])){

        /* WHERE Clause for searching */
        for($i=0 ; $i<count($_POST['columns']);$i++){
            $column = $_POST['columns'][$i]['data'];//we get the name of each column using its index from POST request
            $where[]="$column like '%".$_POST['search']['value']."%'";
        }
        $where = "WHERE ".implode(" OR " , $where);// id like '%searchValue%' or name like '%searchValue%' ....
        /* End WHERE */

        $sql = sprintf("SELECT * FROM %s %s", MyTable , $where);//Search query without limit clause (No pagination)

        $recordsFiltered = count(getData($sql));//Count of search result

        /* SQL Query for search with limit and orderBy clauses*/
        $sql = sprintf("SELECT * FROM %s %s ORDER BY %s %s limit %d , %d ", MyTable , $where ,$orderBy, $orderType ,$start,$length  );
        $data = getData($sql);
    }
    /* END SEARCH */
    else {
        $sql = sprintf("SELECT * FROM %s ORDER BY %s %s limit %d , %d ", MyTable ,$orderBy,$orderType ,$start , $length);
        $data = getData($sql);

        $recordsFiltered = $recordsTotal;
    }

    /* Response to client before JSON encoding */
    $response = array(
        "draw" => intval($draw),
        "recordsTotal" => $recordsTotal,
        "recordsFiltered" => $recordsFiltered,
        "data" => $data
    );

    echo json_encode($response);

} else {
    echo "NO POST Query from DataTable";
}
?>

和它的mysql表

CREATE TABLE IF NOT EXISTS `bond` (
`id` bigint(255) AUTO_INCREMENT ,
`simno` varchar(255) NOT NULL,
 `city` varchar(255) NOT NULL,
`newno` varchar(255) NOT NULL,
`time` varchar(255) NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1;

0 个答案:

没有答案