DataTable:服务器端 - TypeError:无法读取未定义的属性“length”

时间:2016-10-19 08:08:03

标签: jquery database datatable datatables loading

我正在将我的json脚本转换为服务器端脚本来绘制表格,因为当我有500个数据时我开始使用DataTable,但现在我有超过1000个数据。

我正在关注本教程https://coderexample.com/datatable-demo-server-side-in-phpmysql-and-ajax/,但我收到错误:“TypeError:无法读取未定义的属性'长度'”。

怎么了?我很抱歉,但我是新手。

search_user.php

if ($type=="search"){
    $requestData= $_REQUEST;
    $columns = array(
        0 =>'id',
        1 =>'email',
        2 =>'nome',
        3 =>'cognome',
        4 =>'lingua',
        5 =>'unsubscribe'
    );
    $sql = "SELECT id,email,nome,cognome,lingua,unsubscribe FROM newsletter_utenti";
    $query=mysqli_query($db, $sql) or die();
    $totalData = mysqli_num_rows($query);
    $totalFiltered = $totalData;

    $data = array();
    while( $row=mysqli_fetch_array($query) ) {
        $nestedData=array();
        $nestedData[] = $row["id"];
        $nestedData[] = $row["email"];
        $nestedData[] = $row["nome"];
        $nestedData[] = $row["cognome"];
        $nestedData[] = $row["lingua"];
        $nestedData[] = $row["unsubscribe"];
        $data[] = $nestedData;
    }
    $json_data = array(
                "draw"            => intval( $requestData['draw'] ),
                "recordsTotal"    => intval( $totalData ),
                "recordsFiltered" => intval( $totalFiltered ),
                "data"            => $data
                );

    echo json_encode($json_data);
}

JS脚本

var dataTable = $('#contacts-grid').DataTable( {
    "processing": true,
    "serverSide": true,
    "ajax":{
      url : "search_user.php",
      type: "post",
      data: {type:'search'},
      error: function(){  // error handling
        alert("error");

      }
    }
  } );

HTML

<table class="table table-responsive filter-head" id="contacts-grid">
                  <thead>
                    <tr>
                      <th>ID</th>
                      <th>E-mail</th>
                      <th>Nome</th>
                      <th>Cognome</th>
                      <th>Lingua</th>
                      <th>Stato</th>
                    </tr>
                  </thead>
                  <tbody></tbody>
                </table>

1 个答案:

答案 0 :(得分:0)

我已删除条件 $ type ==“search”,现在可以了!