Invalid JSON response

Invalid JSON response

AndreDehCastroAndreDehCastro Posts: 12Questions: 1Answers: 0

Hey guys,

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 )
);*/

require( 'ssp.class.php' );

$d = SSP::simple( $_GET, $sql_details, $table, $primaryKey, $columns );

var_dump( $d );

echo json_encode( $d );

What am I doing wrong?

This question has an accepted answers - jump to answer

Answers

  • tangerinetangerine Posts: 3,365Questions: 39Answers: 395

    " Invalid JSON response." means exactly what it says. What result do you have after you followed the diagnostic steps provided in the error message?

  • AndreDehCastroAndreDehCastro Posts: 12Questions: 1Answers: 0

    Tangerine,

    I know it means exactly what it says. But my point is that I'm using DataTables scripts, all I did was change the columns info and database config so it matches my needs. I get theses errors and I have no clue on how to solve this. I've seen so many people also have the same problem but so far I couldn't find any answer.

  • tangerinetangerine Posts: 3,365Questions: 39Answers: 395

    What result do you have after you followed the diagnostic steps provided in the "Invalid json" error message? That information is there to help you.

  • AndreDehCastroAndreDehCastro Posts: 12Questions: 1Answers: 0

    All it says is to visit a link ( http://datatables.net/tn/1 ). Did you see the scripts and errors I posted along the questions? Do you have any suggestion about the codes and errors?

  • AndreDehCastroAndreDehCastro Posts: 12Questions: 1Answers: 0

    Just to be clear, I appreciate your replies!

    But visiting the link the error tells me to doesn't help me because there in the page it doesn't say how to solve the problem. It only brings meaning and diagnosis as you can check on the very page.

    Please help me!!!!

  • tangerinetangerine Posts: 3,365Questions: 39Answers: 395

    So - after you visited the link - what is the result?

    Also you have a var_dump() in your server-side processing script, which isn't helping.

    With regard to the SQL error, you will have to show your SQL statement to get any help with that.

  • AndreDehCastroAndreDehCastro Posts: 12Questions: 1Answers: 0

    The result is I didn't find any clues on how to fix it...lol

    The var_dump() as just for a test and I forgot to remove it. Now that block of code is like the main code from DataTables example:


    require( 'ssp.class.php' );

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


    My question is, is there any mistake on the codes I've presented? And also, why am I having problems since I'm using the very DataTables files and scripts?

  • kthorngrenkthorngren Posts: 21,299Questions: 26Answers: 4,945
    edited August 2017

    Disregard my response.

    Kevin

  • allanallan Posts: 63,449Questions: 1Answers: 10,465 Site admin

    Can you show me the response from the server (i.e. from the network tab) when you first load the page please?

    The tech note tells you want the error means when the client-side sees invalid JSON - but it could be virtually anything on the server-side that causes it. Hopefully the initial data will help in that regard.

    Allan

  • AndreDehCastroAndreDehCastro Posts: 12Questions: 1Answers: 0

    Allan, thank you so much for replying!

    When the page first loads I have these two information:

    1) Console Log

    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)

    2) Network Tab

    <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>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, 10' at line 5"}

    Do you need any more information?

    Thanks in advance for your help!!!

  • allanallan Posts: 63,449Questions: 1Answers: 10,465 Site admin

    Where did you get the SSP class that you are using from? This is the latest code and there isn't anything on line 116.

    Could you try using the SSP class from that file please?

    Thanks,
    Allan

  • AndreDehCastroAndreDehCastro Posts: 12Questions: 1Answers: 0

    Hey Allan

    I was using the ssp.class.php file from the download I've made from DataTables website. The file path is (DataTables-1.10.15\examples\server_side\scripts\ssp.class.php)

    Now, I've also tried to use the file you've mentioned above and I still get errors on the first page load and each search field keyup, those are:

    1) Console

    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)

    2) Network

    <br />
    <b>Notice</b>: Undefined offset: 0 in <b>C:\wamp64\www\prevmais\sistema\app\server\ssp.class.php</b> on line <b>103</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>152</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>152</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, 10' at line 5"}

    Whatelse could be responsible for these errors?

    Thank you so much Allan for your help!

    André de Castro

  • allanallan Posts: 63,449Questions: 1Answers: 10,465 Site admin

    Line 103 in the file I linked to is empty, so there must be some discrepancy between the file being used, and the one linked to.

    I'm trying to determine what line of code the error is happening on, so I can see if there is anything obvious that would be triggering that.

    If you look at ssp.class.php in your own code Editor, what is on line 103?

    Allan

  • AndreDehCastroAndreDehCastro Posts: 12Questions: 1Answers: 0

    Hey Allan, I answered your last question but my comment wasn't approved yet.

    Did anything happen? Have you already seen it?

    André

  • AndreDehCastroAndreDehCastro Posts: 12Questions: 1Answers: 0

    So, I'm gonna post it again!

    The discrepancy is that I was copying all the code from the link you mentioned from GitHub, it seems like some linebreaks were vanishing while I was pasting it. Now I have saved the file and I still get errors, those are:

    1) Console

    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)

    2) Network Tab

    <br />
    <b>Notice</b>: Undefined offset: 0 in <b>C:\wamp64\www\prevmais\sistema\app\server\ssp.class.php</b> on line <b>124</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>184</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>184</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, 10' at line 5"}

    Once again, I thank you so much for the effort you're making to help me!

    I'm positive that now you'll be able to see the lines with errors!

    André de Castro

  • allanallan Posts: 63,449Questions: 1Answers: 10,465 Site admin

    I'm not sure why your other post didn't get through - sorry about that. I've marked your account so it won't happen again though.

    The error message suggests that there is no columns information being sent to the server as part of the server-side processing request. I don't believe I've seen that issue before.

    Can you use the debugger on your page immediately after the page is loaded and then you've dismissed the error. That should let me see what is being sent to the server.

    Or even better would be a link to the page showing the issue.

    Allan

  • AndreDehCastroAndreDehCastro Posts: 12Questions: 1Answers: 0

    Hey Allan, Thank you for you effort!

    I used the debugger because I only have this project on my localhost yet.

    http://debug.datatables.net/iyahip

    But in case you can't find anything interesting in the link I've mentioned above, I'll try to upload the files and database to some server so you can see it!

    Thank you SO MUCH for your help Allan!

    André

  • allanallan Posts: 63,449Questions: 1Answers: 10,465 Site admin

    Something weird going on. The debugger says:

            "responseText": "",
            "status": 200,
            "statusText": "OK"
    

    is the response from the server. i.e. it was 200 OK, but there was no data returned!

    If you are able to upload the files, that would be really helpful.

    Thanks,
    Allan

  • AndreDehCastroAndreDehCastro Posts: 12Questions: 1Answers: 0

    Hey Allan, on this link you can see what I've uploaded.

    Hope you can find what's causing those errors!

    Thanks in advance!

    André

  • allanallan Posts: 63,449Questions: 1Answers: 10,465 Site admin

    Thanks!

    So I'm seeing nothing at all in the response from the server to the Ajax request. No error information - nothing. That's why DataTables is giving an error.

    Could you add this at the top of your server_side.php file please?

    error_reporting(E_ALL);
    ini_set('display_errors', '1');
    

    Thanks,
    Allan

  • AndreDehCastroAndreDehCastro Posts: 12Questions: 1Answers: 0

    Hey Allan, I don't know if what I did is correct, but it solved my problem for now. I changed the ssp.class.php, I included a charset utf-8 on the PDO connection like this:

    387 $db = @new PDO(
    388             "mysql:host={$sql_details['host']};dbname={$sql_details['db']};charset=utf8",
    389             $sql_details['user'],
    390             $sql_details['pass'],
    391             array( PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION )
    392         ); 
    

    Thank you so much for your help and I hope that others who have the same issue as me can find this discussion and solve it! It may be the real cause but it's working now! lol

  • allanallan Posts: 63,449Questions: 1Answers: 10,465 Site admin
    Answer ✓

    That will do nicely. UTF8 is the way to go!

    Allan

  • pacotstpacotst Posts: 1Questions: 0Answers: 0

    Hi, I also have following error message:
    DataTables warning: table id=gp_ob_table - Invalid JSON response. For more information about this error, please see http://datatables.net/tn/1

    This error only occurs when my data file contains a field with a name with special caracters!
    e.g. if it says "Daniels" I don't have an error message, but I do have one if I write the name correctly as "Daniëls"!

    Can someone fix this error?

  • colincolin Posts: 15,240Questions: 1Answers: 2,599

    Hi @pacotst ,

    Did you follow the suggestions regarding UTF8 in this thread? It should sort you out if you do.

    Cheers,

    Colin

  • davkissdavkiss Posts: 1Questions: 0Answers: 0

    I had the same problem, and it cames from encoding UTF-8.

    When adding "charset=utf8" to my PDO Connexion, it works !

    Thanks @AndreDehCastro !!!

This discussion has been closed.