Datatables 'Show All' lengthMenu throws invalid json error
Datatables 'Show All' lengthMenu throws invalid json error
My dataTables throws 'INVALID JSON' error sporadically when I select 'All' from lengthMenu select input. Does any one know what is causing the problem. The following is my dataTables script:
var dataTable = $('#info-table').DataTable({
dom: 'lfrtipB',
processing: true,
serverSide: true,
orderable: false,
ajax: {
url: 'core.php',
type: 'POST',
data: {action: 'fetch'},
dataType: 'json',
},
searchable: true,
pageLength: 10,
lengthMenu: [[10, 20, 50, 100, -1], [10, 20, 50, 100, "All"]],
});
**The following is also my serverside process:
**
$query = 'SELECT * from archive ';
if (isset($_POST['search']['value'])) {
$query .= 'WHERE sid LIKE "%' . $_POST["search"]["value"] . '%" ';
}
if (isset($_POST['length']) and $_POST['length'] != -1) {
$query .= 'LIMIT ' . $_POST["start"] . ', ' . $_POST["length"] . ' ';
}
$statement = $conn->prepare($query);
$statement->execute();
$result = $statement->fetchAll();
$filteredrows = $statement->rowCount();
$data = array();
foreach ($result as $row) {
$subarray = array();
$subarray[] = $row['sid'];
$subarray[] = $row['fname'] .' '. $row['lname'];
$data[] = $subarray;
}
$output = array(
'draw' => intval($_POST['draw']),
'recordsTotal' => $filteredrows,
'recordsFiltered' => $dbs->getTotalRecords('students'),
'data' => $data
);
echo json_encode($output);
exit();
**Some Points
**
I am using dataTables version 1.10
It just sometimes throws invalid json error (yet, as a tiny bug, it should be evaded).
When error is thrown, consulting Chrome's inspect panel, the json is returned successfully; however, it throws the invalid json error.
It is not just 'Show All' option, very rarely lengthmenu '100' also encounters the same problem. But most of the time, it works properly.
the dataTables never runs into error when I choose 10, 20, and 50 from lengthMenu select input.
There is no errors recorded in my 'PHP error log' file.
I have about 1000 rows in my dataTables.
Nothing special regarding the problem is available at StackOverflow of dataTables website.
Any help would be much appreciated!
Answers
When the error occurs follow the troubleshooting steps provided at the link provided in the error:
https://datatables.net/manual/tech-notes/1
Did you use https://jsonlint.com/ to validate the JSON response?
Do you really need to use server side processing with 1000 rows?
Kevin