dataTable not doesnt display ajax data correct

2019-08-20 13:56发布

I am using DataTables and want to use Ajax to load my data. But I get this error when I load the page:

DataTables warning: table id=example - Requested unknown parameter 'DocentID' for row 0, column 0. For more information about this error, please see http://datatables.net/tn/4

I dont know what i'm doing wrong here is my Datatables script:

$(document).ready(function() {
  $("#example").DataTable({
    "ajax": {
      "url": "assets/includes/ajax.php",
      "dataSrc": ""
    },
    "columns": [
        {"data": "DocentID"}
    ]
  });
});

And my ajax.php file + what it echos

include_once('DB_Handler.php');

$result = mysqli_query($mysqli, "SELECT DocentID FROM gebruiker");

$rows = array();
while ( $row = mysqli_fetch_array($result))
{
    $rows[] = json_encode($row);
}

echo json_encode($rows);

The array :

["{\"0\":\"0\",\"DocentID\":\"0\"}","{\"0\":\"67\",\"DocentID\":\"67\"}","{\"0\":\"0\",\"DocentID\":\"0\"}","{\"0\":\"0\",\"DocentID\":\"0\"}"]

1条回答
Juvenile、少年°
2楼-- · 2019-08-20 14:48

Try removing the first json_encode(). There's no need to encode it twice.

include_once('DB_Handler.php');

$result = mysqli_query($mysqli, "SELECT DocentID FROM gebruiker");

$rows = array();
while ( $row = mysqli_fetch_array($result))
{
    $rows[] = $row;
}

echo json_encode($rows);
查看更多
登录 后发表回答