DataTables Invalid JSON response

那年仲夏 提交于 2021-02-10 19:40:36

问题


I'm having some trouble with DataTables.

I'm trying to use the server-side script to make data loading faster in a project, but I keep getting errors.

When I load the page, the first error I get is:

datatables.min.js:86 Uncaught Error: DataTables warning: table id=lista_pedidos - Invalid JSON response. For more information about this error, please see http://datatables.net/tn/1
at K (datatables.min.js:86)
at Object.error (datatables.min.js:48)
at i (jquery.js:2)
at Object.fireWith [as rejectWith] (jquery.js:2)
at A (jquery.js:4)
at XMLHttpRequest.<anonymous> (jquery.js:4)

When I type anything on the search field I get the same error in the logs and in the tab "Network" I get the following error:

<br />
<b>Notice</b>: Undefined offset: 0 in <b>C:\wamp64\www\prevmais\sistema\app\server\ssp.class.php</b> on line <b>116</b><br />
<br />
<b>Notice</b>: Undefined offset: 0 in <b>C:\wamp64\www\prevmais\sistema\app\server\ssp.class.php</b> on line <b>162</b><br />
<br />
<b>Notice</b>: Undefined offset: 1 in <b>C:\wamp64\www\prevmais\sistema\app\server\ssp.class.php</b> on line <b>162</b><br />
<br />
<b>Notice</b>: Undefined offset: 0 in <b>C:\wamp64\www\prevmais\sistema\app\server\ssp.class.php</b> on line <b>176</b><br />
<br />
<b>Notice</b>: Undefined offset: 1 in <b>C:\wamp64\www\prevmais\sistema\app\server\ssp.class.php</b> on line <b>176</b><br />
{"error":"An SQL error occurred: SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'LIMIT 0, 100' at line 5"}

These are my scripts:

1) Initializing DataTables:

$(document).ready(function() {

$.fn.dataTable.ext.errMode = 'throw';

$('#lista_pedidos').DataTable( {
    "processing": true,
    "serverSide": true,
    "ajax": {
        url: "/app/server/server_side.php",
        dataType: 'JSON'
    }
});

2) Server-side script:

<?php

header('Content-Type: application/json');

/*
* DataTables example server-side processing script.
*
* Please note that this script is intentionally extremely simply to show how
* server-side processing can be implemented, and probably shouldn't be used as
* the basis for a large complex system. It is suitable for simple use cases as
* for learning.
*
* See http://datatables.net/usage/server-side for full details on the server-
* side processing requirements of DataTables.
*
* @license MIT - http://datatables.net/license_mit
*/

/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* Easy set variables
*/

// DB table to use
$table = 'pedidos';

// Table's primary key
$primaryKey = 'id';

// Array of database columns which should be read and sent back to DataTables.
// The db parameter represents the column name in the database, while the dt
// parameter represents the DataTables column identifier. In this case simple
// indexes

$columns = array(
array( 'db' => 'id', 'dt' => 0 ),
array(
'db' => 'pedidos_data',
'dt' => 1,
'formatter' => function( $d, $row ) {
return date( 'jS M y', strtotime($d));
}
),
array( 'db' => 'pedidos_nome', 'dt' => 2 ),
array( 'db' => 'pedidos_cpf', 'dt' => 3 ),
array( 'db' => 'pedidos_rg', 'dt' => 4 ),
array( 'db' => 'pedidos_nascimento', 'dt' => 5 ),
array( 'db' => 'pedidos_status', 'dt' => 6 ),

);

// SQL server connection information
$sql_details = array(
'user' => 'root',
'pass' => '',
'db' => 'prevmaisaude',
'host' => 'localhost'
);

/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* If you just want to use the basic configuration for DataTables with PHP
* server-side, there is no need to edit below this line.
*/

require( 'ssp.class.php' );

echo json_encode(
SSP::simple( $_GET, $sql_details, $table, $primaryKey, $columns )
);

What am I doing wrong?

来源:https://stackoverflow.com/questions/45678791/datatables-invalid-json-response

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!