Uncaught TypeError: Cannot read property 'length' of undefined when trying to populate responsive datatable using PHP?
Uncaught TypeError: Cannot read property 'length' of undefined when trying to populate responsive datatable using PHP?
我正在尝试使用对 PHP 脚本的 AJAX 请求填充响应数据表,响应以 JSON_encode 格式返回,我可以在 XHR 中看到响应请求:
["abc","def","ght","jkl"]
这是我使用的代码:
<table class="table table-striped table-bordered table-hover" id="dataTables-example">
<thead>
<tr>
<th>Name</th>
</tr>
</thead>
<tfoot>
<tr>
<th>Name</th>
</tr>
</tfoot>
</table>
$('#dataTables-example').DataTable({
responsive: true,
"ajax": "search_autocomplete.php",
});
这是 PHP 脚本:
if ($result->num_rows >0) {
// output data of each row
while($row = $result->fetch_assoc()) {
$list[] =$row['name'];
}
echo json_encode( $list );
}
设置JsonHeader
header('Content-type: application/json');
echo json_encode( $list );
您还应该在 while
循环之前定义变量 $list
。如果未定义它 return 只有姓氏。
$list = []
When using just a string value,至少,DataTables 的 ajax
选项期望响应被包装在另一个对象中:
Note that DataTables expects the table data to be an array of items in the data
parameter of the object ...
{
"data": [
// row 1 data source,
// row 2 data source,
// etc
]
}
要得到这个,你可以在编码之前将 $list
包装在另一个 array()
中:
echo json_encode( array( data => $list ) );
当您想插入数组数据源时,即不是对象文字,源必须是数组的数组:
[["abc"],["def"],["ght"],["jkl"]]
$('#dataTables-example').DataTable({
"ajax": {
url: "search_autocomplete.php",
dataSrc: ''
}
});
if ($result->num_rows >0) {
while($row = $result->fetch_assoc()) {
$list[] = array($row['name']); //<----
}
echo json_encode($list);
}
如果您使用 Jonathan 的建议,情况也是如此,json_encode( array(data => $list))
- 您仍然需要将每个项目包装到一个数组中,否则您会得到 a
、d
, g
等因为 dataTables 访问每个字符串作为它期望的数组,每个字符被视为一个数组项,一列的数据。
if ($result->num_rows >0) {
while($row = $result->fetch_assoc()) {
$list[] = array($row['name']); //<----
}
echo json_encode(array('data' => $list));
}
$('#dataTables-example').DataTable({
"ajax": "search_autocomplete.php"
});
我正在尝试使用对 PHP 脚本的 AJAX 请求填充响应数据表,响应以 JSON_encode 格式返回,我可以在 XHR 中看到响应请求:
["abc","def","ght","jkl"]
这是我使用的代码:
<table class="table table-striped table-bordered table-hover" id="dataTables-example">
<thead>
<tr>
<th>Name</th>
</tr>
</thead>
<tfoot>
<tr>
<th>Name</th>
</tr>
</tfoot>
</table>
$('#dataTables-example').DataTable({
responsive: true,
"ajax": "search_autocomplete.php",
});
这是 PHP 脚本:
if ($result->num_rows >0) {
// output data of each row
while($row = $result->fetch_assoc()) {
$list[] =$row['name'];
}
echo json_encode( $list );
}
设置JsonHeader
header('Content-type: application/json');
echo json_encode( $list );
您还应该在 while
循环之前定义变量 $list
。如果未定义它 return 只有姓氏。
$list = []
When using just a string value,至少,DataTables 的 ajax
选项期望响应被包装在另一个对象中:
Note that DataTables expects the table data to be an array of items in the
data
parameter of the object ...{ "data": [ // row 1 data source, // row 2 data source, // etc ] }
要得到这个,你可以在编码之前将 $list
包装在另一个 array()
中:
echo json_encode( array( data => $list ) );
当您想插入数组数据源时,即不是对象文字,源必须是数组的数组:
[["abc"],["def"],["ght"],["jkl"]]
$('#dataTables-example').DataTable({
"ajax": {
url: "search_autocomplete.php",
dataSrc: ''
}
});
if ($result->num_rows >0) {
while($row = $result->fetch_assoc()) {
$list[] = array($row['name']); //<----
}
echo json_encode($list);
}
如果您使用 Jonathan 的建议,情况也是如此,json_encode( array(data => $list))
- 您仍然需要将每个项目包装到一个数组中,否则您会得到 a
、d
, g
等因为 dataTables 访问每个字符串作为它期望的数组,每个字符被视为一个数组项,一列的数据。
if ($result->num_rows >0) {
while($row = $result->fetch_assoc()) {
$list[] = array($row['name']); //<----
}
echo json_encode(array('data' => $list));
}
$('#dataTables-example').DataTable({
"ajax": "search_autocomplete.php"
});