Invalid JSON Response from jQuery Server Side

Invalid JSON Response from jQuery Server Side

User123456User123456 Posts: 57Questions: 15Answers: 0
edited July 2017 in Free community support

I copied the server-side example files from github.com/DataTables/DataTables/tree/master/examples/server_side:
the HTML file I used was simple.html and the scripts: server_processing.php and ssp.class.php, and I modified the database configuration to my specifications. I inserted mysql.sql (copied from the same location) in my DB also without modification.

They are in the same DB. If I use the default configuration(same $sql_details and $collumns) it works fine. Howerever if I change the $columns(same $sql_details, but diferent $collumns), it gives me the creeping error message:

DataTables warning: table id=example - Invalid JSON response. For more information about this error, please see http://datatables.net/tn/1

I followed the advice in http://datatables.net/tn/1 but the network response didn't display the faulty json format but the message: This request has no response data available.

https://i.stack.imgur.com/fk3vo.png

I used the Data Tables debugger which returned the reference: debug.datatables.net/ujutog.

I am greatly thankful if somebody could point out an error, cause there is no response available.

I asked question in Stackoverflow, but there was "no preview answer available".
https://stackoverflow.com/questions/44849202/invalid-json-response-from-datatable-server-side
https://pt.stackoverflow.com/questions/217557/o-que-ocasiona-o-erro-de-json-inv%C3%A1lido
https://pt.stackoverflow.com/questions/216368/resposta-inv%C3%A1lida-json

This question has an accepted answers - jump to answer

«1

Answers

  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin

    but the network response didn't display the faulty json format but the message: This request has no response data available.

    That is faulty JSON :). An empty string on its own is not valid JSON. That's why there is an error from DataTables.

    So the question then becomes, why is the server not responding with anything? Most likely there will be something shown in your server's error log stating what when wrong.

    Allan

  • User123456User123456 Posts: 57Questions: 15Answers: 0
    edited July 2017

    Hello Alan, thanks for the help, I was feeling ignored with this 'issue'.

    Following your advice I searched on C:/Wamp/logs/php_error.log.

    Howerever there was no error related with simple.html, ssp.class.php, or server_processing.php.

    I checked others too, howerever only in acess.log there was some lines, like this:

    ::1 - - [04/Jul/2017:10:47:06 -0300] "GET /Testes/DataTables-master/examples/server_side/scripts/server_processing.php?draw=1&columns%5B0%5D%5Bdata%5D=0&columns%5B0%5D%5Bname%5D=&columns%5B0%5D%5Bsearchable%5D=true&columns%5B0%5D%5Borderable%5D=true&columns%5B0%5D%5Bsearch%5D%5Bvalue%5D=&columns%5B0%5D%5Bsearch%5D%5Bregex%5D=false&columns%5B1%5D%5Bdata%5D=1&columns%5B1%5D%5Bname%5D=&columns%5B1%5D%5Bsearchable%5D=true&columns%5B1%5D%5Borderable%5D=true&columns%5B1%5D%5Bsearch%5D%5Bvalue%5D=&columns%5B1%5D%5Bsearch%5D%5Bregex%5D=false&columns%5B2%5D%5Bdata%5D=2&columns%5B2%5D%5Bname%5D=&columns%5B2%5D%5Bsearchable%5D=true&columns%5B2%5D%5Borderable%5D=true&columns%5B2%5D%5Bsearch%5D%5Bvalue%5D=&columns%5B2%5D%5Bsearch%5D%5Bregex%5D=false&columns%5B3%5D%5Bdata%5D=3&columns%5B3%5D%5Bname%5D=&columns%5B3%5D%5Bsearchable%5D=true&columns%5B3%5D%5Borderable%5D=true&columns%5B3%5D%5Bsearch%5D%5Bvalue%5D=&columns%5B3%5D%5Bsearch%5D%5Bregex%5D=false&columns%5B4%5D%5Bdata%5D=4&columns%5B4%5D%5Bname%5D=&columns%5B4%5D%5Bsearchable%5D=true&columns%5B4%5D%5Borderable%5D=true&columns%5B4%5D%5Bsearch%5D%5Bvalue%5D=&columns%5B4%5D%5Bsearch%5D%5Bregex%5D=false&columns%5B5%5D%5Bdata%5D=5&columns%5B5%5D%5Bname%5D=&columns%5B5%5D%5Bsearchable%5D=true&columns%5B5%5D%5Borderable%5D=true&columns%5B5%5D%5Bsearch%5D%5Bvalue%5D=&columns%5B5%5D%5Bsearch%5D%5Bregex%5D=false&order%5B0%5D%5Bcolumn%5D=0&order%5B0%5D%5Bdir%5D=asc&start=0&length=10&search%5Bvalue%5D=&search%5Bregex%5D=false&_=1499176026879 HTTP/1.1" 200 -

    Extra
    I tried to create a new table called users_demo, with full data in all collumns howerever the error still prevailed.

    Table Collumns:
    id, name, email, city, age, sex, position

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

    C:/Wamp/logs/php_error.log.

    That's probably not the server error log. Does Wamp not have anything like apache/logs/error ?

  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin

    You may also have to enable PHP's error logging if your system has it disabled.

    Allan

  • User123456User123456 Posts: 57Questions: 15Answers: 0

    @tangerine, I looked in C:/Wamp/errors. There are 4 error logs, apache, mysql, acess, and php. Only in acess, there was things related with datatables.

    @allan, PDO and PHP error log are enabled.

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

    Look at Apache's error log. Entries are timed so you can establish which is relevant to your last usage.

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

    Look at Apache's error log. Entries are timed so you can establish which is relevant to your last usage.

  • User123456User123456 Posts: 57Questions: 15Answers: 0
    edited July 2017

    Nothing relevant in apache_error.log @tangerine:

    [Tue Jul 04 15:22:07.917945 2017] [auth_digest:notice] [pid 1884:tid 328] AH01757: generating secret for digest authentication ...
    [Tue Jul 04 15:22:08.027145 2017] [mpm_winnt:notice] [pid 1884:tid 328] AH00455: Apache/2.4.23 (Win64) PHP/5.6.25 configured -- resuming normal operations
    [Tue Jul 04 15:22:08.027145 2017] [mpm_winnt:notice] [pid 1884:tid 328] AH00456: Apache Lounge VC14 Server built: Jul 1 2016 11:43:51
    [Tue Jul 04 15:22:08.027145 2017] [core:notice] [pid 1884:tid 328] AH00094: Command line: 'c:\wamp64\bin\apache\apache2.4.23\bin\httpd.exe -d C:/wamp64/bin/apache/apache2.4.23'
    [Tue Jul 04 15:22:08.027145 2017] [mpm_winnt:notice] [pid 1884:tid 328] AH00418: Parent: Created child process 6228
    [Tue Jul 04 15:22:08.385946 2017] [auth_digest:notice] [pid 6228:tid 152] AH01757: generating secret for digest authentication ...
    [Tue Jul 04 15:22:08.432746 2017] [mpm_winnt:notice] [pid 6228:tid 152] AH00354: Child: Starting 64 worker threads.
    [Tue Jul 04 15:33:55.537043 2017] [:error] [pid 6228:tid 896] [client ::1:52778] script 'C:/wamp64/www/Testes/DataTables-master/examples/server_side/simple.php' not found or unable to stat

    I made a test @allan, to see if PHP was working nice. 'echo $a'-> Undefined Index, error shown in the page and in php error log:

    [04-Jul-2017 18:29:15 UTC] PHP Notice: Undefined variable: a in C:\wamp64\www\Testes\DataTables-master\examples\server_side\simple.php on line 51
    [04-Jul-2017 18:29:15 UTC] PHP Stack trace:
    [04-Jul-2017 18:29:15 UTC] PHP 1. {main}() C:\wamp64\www\Testes\DataTables-master\examples\server_side\simple.php:0

  • User123456User123456 Posts: 57Questions: 15Answers: 0

    I'm not sure but maybe you can see the error, I'm not seeing.

    Simple.html:

    <DOCTYPE html>
    <html>
    <head>
        <meta charset="utf-8">
        <link rel="shortcut icon" type="image/ico" href="http://www.datatables.net/favicon.ico">
        <meta name="viewport" content="initial-scale=1.0, maximum-scale=2.0">
        <title>DataTables example - Server-side processing</title>
        <link rel="stylesheet" type="text/css" href="../../media/css/jquery.dataTables.css">
        <link rel="stylesheet" type="text/css" href="../resources/syntax/shCore.css">
        <link rel="stylesheet" type="text/css" href="../resources/demo.css">
        <style type="text/css" class="init">
        
        </style>
        <script type="text/javascript" language="javascript" src="//code.jquery.com/jquery-1.12.4.js">
        </script>
        <script type="text/javascript" language="javascript" src="../../media/js/jquery.dataTables.js">
        </script>
        <script type="text/javascript" language="javascript" src="../resources/syntax/shCore.js">
        </script>
        <script type="text/javascript" language="javascript" src="../resources/demo.js">
        </script>
        <script type="text/javascript" language="javascript" class="init">
        
    $(document).ready(function() {
        $('#table').DataTable( {
            "processing": true,
            "serverSide": true,
            "ajax": "scripts/server_processing.php"
        } );
    } );
    
        </script>
    </head>
    <body class="dt-example">
        <div class="container">
            <section>
                <div class="demo-html"></div><br>
                <table id="table" class="display" cellspacing="0" width="100%">
                    <thead>
                        <tr>
                            <th>ID</th>
                            <th>Razão Social</th>
                            <th>CNPJ</th>
                            <th>CEP</th>
                            <th>Rua</th>
                            <th>Cidade</th>
                        </tr>
                    </thead>
                </table>
            </section>
        </div>
        <br>
    </body>
    </html>
    

    Server_Processing.php:

    <?php
    
    ini_set('display_errors',1);
    ini_set('display_startup_erros',1);
    error_reporting(E_ALL);
    
    $table = 'tbl_company';
    $primaryKey = 'id_Company';
    
    $columns = array(
        array( 'db' => 'razaoSocial', 'dt' => 0 ),
        array( 'db' => 'cnpj',  'dt' => 1 ),
        array( 'db' => 'cep',   'dt' => 2 ),
        array( 'db' => 'rua',     'dt' => 3 ),
        array( 'db' => 'cidade',   'dt' => 4 ),
        array( 'db' => 'uf',     'dt' => 5 )
    );
    
    $sql_details = array(
        'user' => '',//User
        'pass' => '',//Pass
        'db'   => '',//DB
        'host' => 'localhost'//Host
    );
    
    require( 'ssp.class.php' );
    echo json_encode(
        SSP::simple( $_GET, $sql_details, $table, $primaryKey, $columns )
    );
    

    ssp.class.php is the same as github repo.

  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin

    There is nothing obviously wrong with the code there. What version of PHP is your server running?

    Could you try taking a debug trace and let me know what the debug code is please?

    Thanks,
    Allan

  • User123456User123456 Posts: 57Questions: 15Answers: 0
    edited July 2017

    I used the datatables debugger already, just below the javascript initialization:

    $(document).ready(function() {
        $('#table').DataTable( {
            "processing": true,
            "serverSide": true,
            "ajax": "scripts/server_processing.php"
        } );
    } );
    
    (function() {
        var url = '//debug.datatables.net/bookmarklet/DT_Debug.js';
        if (typeof DT_Debug != 'undefined') {
            if (DT_Debug.instance !== null) {
                DT_Debug.close();
            } else {
                new DT_Debug();
            }
        } else {
            var n = document.createElement('script');
            n.setAttribute('language', 'JavaScript');
            n.setAttribute('src', url + '?rand=' + new Date().getTime());
            document.body.appendChild(n);
        }
    })();
    

    My first result is here:
    debug.datatables.net/ujutog

    And the second one I did now is:
    debug.datatables.net/etizup

    My server is WAMP, and I use Apache 2.4.23, PHP 7.0.10 and MySQL 5.7.14 in a Windows Server 2012.

  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin

    That shows that scripts/server_processing.php is returning 200 OK, and an empty string. Which is weird, since it shouldn't be!

    What happens if you put a simple echo "I am here"; immediately before calling the SSP class, but after the require? Is the echo shown in the return from the server?

    Allan

  • User123456User123456 Posts: 57Questions: 15Answers: 0
    edited July 2017

    Just as you said @allan. The server response is "I am here". Strange that returns this, but not the database response. What I should do?

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

    [Tue Jul 04 15:33:55.537043 2017] [:error] [pid 6228:tid 896] [client ::1:52778] script 'C:/wamp64/www/Testes/DataTables-master/examples/server_side/simple.php' not found or unable to stat

    Is that not relevant?

  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin

    Try putting it after the SSP class. My guess is that it won't be shown any more.

    @tangerine - I think that's just a demo page that isn't present.

    Allan

  • User123456User123456 Posts: 57Questions: 15Answers: 0
    edited July 2017

    @allan, putting echo "I am here!"; after the require of SSP class, shows I am here in the XHR response in the console.

    @tangerine - this was another test I did. It's irelevant for this issue. I am using the simple.html, not the PHP version

  • User123456User123456 Posts: 57Questions: 15Answers: 0

    I'm not sure but maybe the error is related with the database. Here is my tbl_company table @allan:

    Fields in red are those I wish to retrieve.

  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin

    Okay - let's try this:

    require( 'ssp.class.php' );
    
    $d = SSP::simple( $_GET, $sql_details, $table, $primaryKey, $columns );
    
    var_dump( $d );
    
    echo json_encode( $d );
    

    What does the browser show the server is returning now.

    Sorry for the guess work!

    Allan

  • User123456User123456 Posts: 57Questions: 15Answers: 0
    edited July 2017

    It's returning the data @allan. But the error still continues. The data is not fetched in the table:

    C:\wamp64\www\Testes\DataTables-master\examples\server_side\scripts\server_processing.php:24:
    array (size=4)
      'draw' => int 1
      'recordsTotal' => int 2
      'recordsFiltered' => int 2
      'data' => 
        array (size=2)
          0 => 
            array (size=6)
              0 => string 'Metta Contabilidade' (length=19)
              1 => string '14.579.812/1234-78' (length=18)
              2 => string '13631-165' (length=9)
              3 => string 'Rua Santos Dumont' (length=17)
              4 => string 'Pirassununga' (length=12)
              5 => string 'SP' (length=2)
          1 => 
            array (size=6)
              0 => string 'UzmkArtanis Game Tech' (length=21)
              1 => string '42.875.000/1486-82' (length=18)
              2 => string '13630-275' (length=9)
              3 => string 'Rua Sofia Levi' (length=14)
              4 => string 'Aiur' (length=4)
              5 => string 'SP' (length=2)
    {"draw":1,"recordsTotal":2,"recordsFiltered":2,"data":[["Metta Contabilidade","14.579.812\/1234-78","13631-165","Rua Santos Dumont","Pirassununga","SP"],["UzmkArtanis Game Tech","42.875.000\/1486-82","13630-275","Rua Sofia Levi","Aiur","SP"]]}
    

    And JSONLint and JSONParser says that this(above) JSON is valid.

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

    From {"draw": onwards is valid JSON, but what precedes it isn't.
    Do you have any rogue "echo"s or other debug code anywhere?

  • User123456User123456 Posts: 57Questions: 15Answers: 0
  • tangerinetangerine Posts: 3,365Questions: 39Answers: 395

    So what happens if you get rid of the var_dump() ?

  • User123456User123456 Posts: 57Questions: 15Answers: 0

    DataTables warning: table id=example - Invalid JSON response. For more information about this error, please see http://datatables.net/tn/1

  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin

    Try it this way around:

    require( 'ssp.class.php' );
     
    $d = SSP::simple( $_GET, $sql_details, $table, $primaryKey, $columns );
     
    echo json_encode( $d );
    
    var_dump( $d );
    

    The output above shows the var_dump first.

    Can you also use the debugger on it so I can see the data directly.

    Allan

  • User123456User123456 Posts: 57Questions: 15Answers: 0

    Hello @allan. I don't know how, when or who happened, but this morning suddenly my webpage was working nicely when I arrived, with the plugin correct and no errors.

    I'm really amazed on what happened, but this issue has been closed for me, as there are no more erros reporting, not even through alerts or console.

    Thank you, anyway or anyhow you made it works again.

    Regards,
    User

  • bindridbindrid Posts: 730Questions: 0Answers: 119

    Your web server just needed a good nights rest

  • User123456User123456 Posts: 57Questions: 15Answers: 0
    edited July 2017

    Hello again @allan. In my locahost machine everything worked fine. Howerever in the webserver (in production), I was updating there today and appeared:

    DataTables warning: table id=example - Invalid JSON response. For more information about this error, please see http://datatables.net/tn/1

    I did the same as before howerever this time didn't magically fixed. Can you help one more time? The code is almost the same.

    Here is a datatables debug:
    https://debug.datatables.net/ajazig

    The data is returning in the console, but not in the <table>, just as before.

  • User123456User123456 Posts: 57Questions: 15Answers: 0
    edited July 2017

    Here is an example JSON response:

    array(4) { ["draw"]=> int(1) ["recordsTotal"]=> int(59) ["recordsFiltered"]=> int(59) ["data"]=> array(10) { [0]=> array(8) { [0]=> string(1) "0" [1]=> string(47) "A & D Com�rcio de Materiais Did�ticos Ltda - ME" [2]=> string(0) "" [3]=> NULL [4]=> NULL [5]=> string(12) "Pirassununga" [6]=> NULL [7]=> string(2) "53" } [1]=> array(8) { [0]=> string(1) "0" [1]=> string(45) "Adriana Scamillia Com�rcio de Cosm�ticos - ME" [2]=> string(18) "24.905.485/0001-20" [3]=> string(9) "13643-320" [4]=> string(26) "Rua Irene Pegoraro Methner" [5]=> string(12) "Pirassununga" [6]=> string(25) "Jardim Terras de San Jos�" [7]=> string(2) "46" } [2]=> array(8) { [0]=> string(1) "0" [1]=> string(49) "Andre dos Reis Andreotti Com�rcio de Sucatas - ME" [2]=> string(18) "14.669.660/0001-74" [3]=> string(9) "13633-037" [4]=> string(35) "Avenida Cap. Antonio Joaquim Mendes" [5]=> string(12) "Pirassununga" [6]=> string(16) "Jd. Carlos Gomes" [7]=> string(2) "44" } [3]=> array(8) { [0]=> string(1) "0" [1]=> string(35) "Andr� Luis Rodrigues Alimentos - ME" [2]=> string(18) "23.543.447/0001-02" [3]=> NULL [4]=> NULL [5]=> string(12) "Pirassununga" [6]=> NULL [7]=> string(2) "23" } [4]=> array(8) { [0]=> string(1) "1" [1]=> string(28) "Andr�ia Aparecida Fioramonti" [2]=> string(0) "" [3]=> NULL [4]=> NULL [5]=> string(12) "Pirassununga" [6]=> NULL [7]=> string(2) "16" } [5]=> array(8) { [0]=> string(1) "0" [1]=> string(34) "Arnaldo Luiz Pereira Curitiba - ME" [2]=> string(18) "04.828.304/0001-00" [3]=> string(9) "13633-515" [4]=> string(12) "Rua Curitiba" [5]=> string(12) "Pirassununga" [6]=> string(12) "Vila Belmiro" [7]=> string(2) "43" } [6]=> array(8) { [0]=> string(1) "0" [1]=> string(58) "Associa��o de Capoeira de Pirassununga e Interior Paulista" [2]=> string(18) "21.165.305/0001-23" [3]=> string(9) "13631-227" [4]=> string(19) "Rua Francisco Mollo" [5]=> string(12) "Pirassununga" [6]=> string(13) "Jardim Pavesi" [7]=> string(2) "45" } [7]=> array(8) { [0]=> string(1) "1" [1]=> string(23) "Benvindo Flausino Alves" [2]=> string(18) "14.084.994/0001-86" [3]=> NULL [4]=> NULL [5]=> string(12) "Pirassununga" [6]=> NULL [7]=> string(2) "27" } [8]=> array(8) { [0]=> string(1) "1" [1]=> string(24) "Carlos Eduardo Paes - ME" [2]=> string(18) "02.222.209/0001-60" [3]=> string(9) "13631-165" [4]=> string(17) "Rua Santos Dumont" [5]=> string(12) "Pirassununga" [6]=> string(19) "Jardim S�o Valentim" [7]=> string(1) "4" } [9]=> array(8) { [0]=> string(1) "0" [1]=> string(29) "CELIO HALAN AMARO 34684045811" [2]=> string(18) "27.634.017/0001-57" [3]=> string(9) "13630-614" [4]=> string(20) "RUA BENEDITO SAMPAIO" [5]=> string(12) "Pirassununga" [6]=> string(16) "JARDIM MARGARIDA" [7]=> string(2) "33" } } }
    

    My ssp.class is the same as the github, and my <script> tag have this code in the listCompany.php:

    $(document).ready(function() {
                    $('#companyTable').DataTable({
                        "processing": true,
                        "serverSide": true,
                        "ajax": "lib/server_processing.php",
    
                        "columns": [
                                    { "data": 0,
                       "render": function ( data, type, row ) {
                                      var text = "";
                                      if (type == "display") {
                                        if (data == "1") {
                                           text = "<i class='ace-icon fa fa-circle green'></i>";
                                        } else {
                                           text = "<i class='ace-icon fa fa-circle red'></i>";
                                        }
                                        data = text
                                      }
                                      return data;
                         },
                      },
                                    { "data": 1 },
                                    { "data": 2 },
                                    { "data": 3 },
                                    { "data": 4 },
                                    { "data": 5 },
                                    { "data": 6 }
                                ],
    
                        "order": [ 1, "asc" ],
    
                        "columnDefs": [ 
                            { "orderable": false, "targets": 0 },
                            { "orderable": false, "targets": 7 },
                        {
                              "targets": 7,
                              "data": "",
                              "render": function(data, type, full){
                                 return '<a data-toggle="modal" data-target="#infoModal" data-id="' + full[7] + '" id="getCompany" class="blue"><i class="ace-icon fa fa-search-plus bigger-130"></i></a> <a class="red" href="deleteCompany.php?id_Company=' + full[7] + '"><i class="ace-icon fa fa-trash-o bigger-130"></i></a> <a class="orange" data-id="' + full[7] + '" id="blockCompany"><i class="ace-icon fa fa-eye-slash bigger-130"></i></a>';
                              }} ],
    
                        "sScrollX": "100%",
                        "sScrollXInner": "103%",
                    });
                });
    

    My server_processing code is:

    <?php
    $table = 'tbl_company';
    $primaryKey = 'id_Company';
    
    $columns = array(
        array( 'db' => 'ativa',       'dt' => 0 ),
        array( 'db' => 'razaoSocial', 'dt' => 1 ),
        array( 'db' => 'cnpj',        'dt' => 2 ),
        array( 'db' => 'cep',         'dt' => 3 ),
        array( 'db' => 'rua',         'dt' => 4 ),
        array( 'db' => 'cidade',      'dt' => 5 ),
        array( 'db' => 'bairro',      'dt' => 6 ),
        array( 'db' => 'id_Company',  'dt' => 7, 
               'formatter' => function( $id, $row ) {
                 return $id;
            }
        ),
    );
    
    $sql_details = array(
        'user' => 'root',
        'pass' => pass'',
        'db'   => 'db',
        'host' => 'localhost'
    );
    require_once('ssp.class.php');
    echo json_encode(
        SSP::simple( $_GET, $sql_details, $table, $primaryKey, $columns )
    );
    
    
  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin

    array(4) { ["draw"]=> int(1) ["recordsTotal"]=> int(59) ....

    That's not valid JSON, which is why that error is being given.

    Are you using a different version of PHP on your production server? If so, what version?

    Allan

  • RodidiRodidi Posts: 3Questions: 1Answers: 0

    @User123456 are you using some word like "Irmãos" in your database?!

    if yes watch this image and change in your ssp.class.php

This discussion has been closed.