DataTables warning: table id=example - Invalid JSON response.
DataTables warning: table id=example - Invalid JSON response.
I am using DataTables 1.10.21 with Server Side option and 4000 record´s MySQL database. In my localhost environment works fine, but when I try the same in the production server, it fails with the message:
"DataTables warning: table id=example - Invalid JSON response. For more information about this error, please see http://datatables.net/tn/1"
I tried to inspect the site with Firefox, but there´s no output of the JSON data, even when I execute server side script directly.....There´s no PHP error logged.
I also have tried DataTable´s debug option - https://debug.datatables.net/ezafed
Anybody can help me?
server-side-script.php
$table = 'customers_filtrado';
$primaryKey = 'id_houses';
$columns = array(
array(
'db' => 'firstname',
'dt' => 0,
'formatter' => function( $d, $row ) {
$sql = "
SELECT
firstname
FROM
customers
WHERE
id_houses = :id_houses";
$stmt = DB::prepare($sql);
$stmt->bindParam(':id_houses', $row['id_houses'], PDO::PARAM_INT);
$stmt->execute();
$rs = $stmt->fetch(PDO::FETCH_ASSOC);
$firstname = $rs['firstname'];
return "<a href='comments.php?id=".$row['id_houses']."'>".$d."</a>";
}),
array( 'db' => 'lastname', 'dt' => 1 ),
array( 'db' => 'phone', 'dt' => 2 ),
array( 'db' => 'email', 'dt' => 3 ),
array( 'db' => 'city', 'dt' => 4 ),
array( 'db' => 'address', 'dt' => 5 ),
array( 'db' => 'state', 'dt' => 6 ),
array( 'db' => 'zipcode', 'dt' => 7 ),
array( 'db' => 'bedroom', 'dt' => 8 ),
array( 'db' => 'bathroom', 'dt' => 9 ),
array( 'db' => 'square_footage', 'dt' => 10 ),
array( 'db' => 'basement', 'dt' => 11 ),
array( 'db' => 'sewer', 'dt' => 12 ),
array( 'db' => 'situation', 'dt' => 13 ),
array( 'db' => 'sell_keep', 'dt' => 14 ),
array( 'db' => 'date_ymd_hs', 'dt' => 15 ),
array( 'db' => 'id_houses', 'dt' => 16 )
);
// SQL server connection information
$sql_details = array(
'user' => Appconf::DBUSER,
'pass' => Appconf::DBPASSWORD,
'db' => Appconf::DBNAME,
'host' => Appconf::DBHOST
);
require( 'ssp.class.php' );
echo json_encode(
SSP::simple( $_GET, $sql_details, $table, $primaryKey, $columns )
);
report.html
$(document).ready(function () {
$('#example').DataTable({
"processing": true,
"serverSide": true,
ajax: {
url: "/../../admin/server-side-data.php",
type: "GET"
}
});
});
Answers
This is likely the issue - it sounds like no JSON is being returned, which is invalid JSON. Are you able to link to the page so we can see?
Colin
This is the page...
https://www.estateservicing.com/admin/report_customer_list_view.php
You have seventeen columns in your $columns array, but sixteen in your HTML.
EDIT: Not sure that's the primary issue. Take a look at the "network" response as explained in the link http://datatables.net/tn/1.
No, it´s not the issue. My network response is empty.
https://prnt.sc/smxcfp
Yep, agreed, the server script is returning nothing. so you'll need to debug that. Is it connecting to the database correctly? Is it getting the correct values?
Colin
The DB connection works fine and getting the correct values at my local environment, so I supposed it´s not the issue. I generated a debug report and change $_GET to $_POST...
https://debug.datatables.net/agocuc
It looks like your example ( https://www.estateservicing.com/admin/report_customer_list_view.php ) is working now? Do you still have a problem to debug?
Kevin
I have changed the code. On the report.html and server-side-data.php stayed like this:
report.html
server-side-data.php
I believe that what was causing overhead was calling a view "customers_filtrado" instead of "customers" table. directly But I believe that the way I coded this part is not so much efficient ... Is there a better way to work with "INNER JOINS" ?? What is better, do the data transformation on the client side or on the server side, as I did in the id_houses column?