DataTable waring: table id=lodgeList - Invalid JSON response - After transfered to other server

DataTable waring: table id=lodgeList - Invalid JSON response - After transfered to other server

lukasztomlukasztom Posts: 9Questions: 1Answers: 0

Hello everyone
I am here a new and I would like to ask for help regarding one issue regarding "datables ajax"

After transferring it from one server to another, I am facing an issue with my system. We can see the list of records (old_server.jpg)

Upon logging in, you should see a list of lodges displayed via the integration of: https://datatables.net/

The transferred files are now located on the

new server. However, upon logging in, I am encountering an error that states there are no records of lodges and I am receiving the following error message (new_server.jpg):

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

As well In my error_log I am also getting these SESSION errors :

[29-Aug-2024 19:50:41 UTC] PHP Warning: Undefined array key "name" in /home/victor21/public_html/victoria/database/menus.php on line 23
[29-Aug-2024 19:54:39 UTC] PHP Warning: session_start(): open(/var/cpanel/php/sessions/alt-php70/sess_1462d20a1b1fdfcb05fb6980bc96ee71, O_RDWR) failed: No such file or directory (2) in /home/victor21/public_html/victoria/database/lodge.php on line 3
[29-Aug-2024 19:54:39 UTC] PHP Warning: session_start(): Failed to read session data: files (path: /var/cpanel/php/sessions/alt-php70) in /home/victor21/public_html/victoria/database/lodge.php on line 3
[29-Aug-2024 19:54:39 UTC] PHP Warning: Undefined array key "name" in /home/victor21/public_html/victoria/database/menus.php on line 23


I asked my new server provider but he messaged me back that their server is configured correctly and no issue from their site.

I will grateful for any advice to fix it
Many thanks
Lukasz

This question has an accepted answers - jump to answer

Answers

  • kthorngrenkthorngren Posts: 21,391Questions: 26Answers: 4,961

    There isn't anything Datatables can do to fix these errors. They are errors coming from your server script. It will require debugging on the server to track down the errors. Without seeing any configuration info its impossibly to guess which, if any, of those errors are resulting in the JSON error.

    Did you follow the troubleshooting steps provided at the link in the error?
    https://datatables.net/manual/tech-notes/1

    What is in the XHR response?

    Post your Datatables config.

    Kevin

  • lukasztomlukasztom Posts: 9Questions: 1Answers: 0

    Hi Kevin
    Thank you for your quick response.
    Yes, I followed the troubleshooting steps but without luck.
    My XHR response is (new_server.jpg):

    " No response data available for this request"

    Here is datatables config:

    $(document).ready(function() {

    var userdataTable = $('#lodgeList').DataTable( {
        lengthChange: false,
        processing: true,
        serverSide: true,
        order: [],
    
        ajax: {
            url: "action.php",
            type: "POST",
            data: { action: 'lodgeList' },
            dataType: "json"
        },
    
    
         columnDefs: [{
            "target": 0,
            "orderable": false
        }], 
         lengthMenu: [
        [10, 25, 50, -1],
        [10, 25, 50, 'All']
        ], 
        pageLength: 50, 
        'rowCallback': function(row, data, index) {
            $(row).find('td').addClass('align-middle')
            $(row).find('td:eq(0), td:eq(16)').addClass('text-center')
            $(row).find('td:eq(15)').addClass('text-end')
        },
        dom: "Bfrtip",
        buttons: [
            { text: 'copy',
            extend: 'copyHtml5',
            exportOptions: {
            columns: ':visible:not(.not-export-col)'
                }
            },
            { text: 'excel',
            extend: 'excelHtml5',
            exportOptions: {
            columns: ':visible:not(.not-export-col)'
                }
            },
            { text: 'pdf',
            extend: 'pdfHtml5',
            exportOptions: {
            columns: ':visible:not(.not-export-col)'
                }
            }
            , 'colvis'
            ],
    
       // bDestroy: true
    });
    

    });

    I appreciate your help
    Lukasz

  • kthorngrenkthorngren Posts: 21,391Questions: 26Answers: 4,961
    edited August 30

    The errors you posted above seem to be from other scripts since your ajax.url is action.php. They might eb indirectly related depending on what action.php does. The error is due to not receiving anything in the response.

    Are you using a Datatables supplied server side processing script or your own script?

    The action.php script will need to be debugged to determine why its not sending JSON in the response.

    Kevin

  • kthorngrenkthorngren Posts: 21,391Questions: 26Answers: 4,961

    I looked at the screenshot again for the Status code but its cutoff. What is the response code?

    Kevin

  • lukasztomlukasztom Posts: 9Questions: 1Answers: 0
    edited August 30

    Thank you for your reply
    So in action.php I have this code:

    <?php
    session_start();
    include 'Inventory.php';
    $inventory = new Inventory();
    if(!empty($_GET['action']) && $_GET['action'] == 'logout') {
    session_unset();
    session_destroy();
    header("Location:index.php");
    }

    if(!empty($_POST['action']) && $_POST['action'] == 'lodgeList') {
    $inventory->getLodgeList();
    }

    <?php > ?>

    and inside Inventory.php I have this class :
    of course, on the top of this Inventory.php, I got configuration with database: user, password, database and etc

    public function getLodgeList(){     
    
        $sqlQuery = "SELECT * FROM lodge ";
        if(!empty($_POST["search"]["value"])){
    
            $sqlQuery .= 'WHERE no LIKE "%'.$_POST["search"]["value"].'%" ';
            $sqlQuery .= 'OR name LIKE "%'.$_POST["search"]["value"].'%" ';
            $sqlQuery .= 'OR address1 LIKE "%'.$_POST["search"]["value"].'%" ';
            $sqlQuery .= 'OR address2 LIKE "%'.$_POST["search"]["value"].'%" ';
            $sqlQuery .= 'OR town LIKE "%'.$_POST["search"]["value"].'%" ';
            $sqlQuery .= 'OR post_code LIKE "%'.$_POST["search"]["value"].'%" ';
            $sqlQuery .= 'OR province LIKE "%'.$_POST["search"]["value"].'%" ';
            $sqlQuery .= 'OR country LIKE "%'.$_POST["search"]["value"].'%" ';
            $sqlQuery .= 'OR mon LIKE "%'.$_POST["search"]["value"].'%" ';
            $sqlQuery .= 'OR tue LIKE "%'.$_POST["search"]["value"].'%" ';
            $sqlQuery .= 'OR wed LIKE "%'.$_POST["search"]["value"].'%" ';
            $sqlQuery .= 'OR thu LIKE "%'.$_POST["search"]["value"].'%" ';
            $sqlQuery .= 'OR fri LIKE "%'.$_POST["search"]["value"].'%" ';
            $sqlQuery .= 'OR sat LIKE "%'.$_POST["search"]["value"].'%" ';
            $sqlQuery .= 'OR sun LIKE "%'.$_POST["search"]["value"].'%" ';
            $sqlQuery .= 'OR notes LIKE "%'.$_POST["search"]["value"].'%" ';
    
        }
    
        if(!empty($_POST["order"])){
            $sqlQuery .= 'ORDER BY '.$_POST['order']['0']['column'].' '.$_POST['order']['0']['dir'].' ';
        } else {
            $sqlQuery .= 'ORDER BY province ASC ';
        }
    
        $result = mysqli_query($this->dbConnect, $sqlQuery);
        $numRows = mysqli_num_rows($result);
        $customerData = array();    
        while( $customer = mysqli_fetch_assoc($result) ) {      
            $customerRows = array();
            $customerRows[] = $customer['lodgeid'];
            $customerRows[] = $customer['no'];
            $customerRows[] = $customer['name'];
            $customerRows[] = $customer['address1'];
            $customerRows[] = $customer['address2'];
            $customerRows[] = $customer['town'];
            $customerRows[] = $customer['post_code'];
            $customerRows[] = $customer['province'];
            $customerRows[] = $customer['country'];         
            $customerRows[] = $customer['mon']; 
            $customerRows[] = $customer['tue'];
            $customerRows[] = $customer['wed'];
            $customerRows[] = $customer['thu'];
            $customerRows[] = $customer['fri'];
            $customerRows[] = $customer['sat'];
            $customerRows[] = $customer['sun'];
            $customerRows[] = $customer['notes'];   
            //$customerRows[] = '<button type="button" name="update" id="'.$customer["lodgeid"].'" class="btn btn-primary btn-sm rounded-0 update" title="update"><i class="fa fa-edit"></i></button>';
            $customerRows[] = '';
            $customerData[] = $customerRows;
        }
    
        $output = array(
            "draw"              =>  intval($_POST["draw"]),
            "recordsTotal"      =>  $numRows,
            "recordsFiltered"   =>  $numRows,
            "data"              =>  $customerData
        );
    echo json_encode($output);
    
    }
    

    Many thanks for more advice
    Lukasz

  • allanallan Posts: 63,602Questions: 1Answers: 10,486 Site admin

    Have you check the server's PHP error log?

  • lukasztomlukasztom Posts: 9Questions: 1Answers: 0

    Yes, checked and don't have none server's PHP errors

  • allanallan Posts: 63,602Questions: 1Answers: 10,486 Site admin

    " No response data available for this request"

    That means that the server isn't returning anything (which in turn isn't valid JSON - hence the error). If there is no PHP error that is stopping the response, then you need to check the logic in your PHP script to see why it isn't returning anything. It isn't immediately apparent to me from the above function why it wouldn't be returning anything.

    Allan

  • lukasztomlukasztom Posts: 9Questions: 1Answers: 0

    Thank you for reply
    I dont know if read my first post that on my old server everything was working but when I transferred to another server (krystal.io) is not working so that confused me….
    Lukasz

  • allanallan Posts: 63,602Questions: 1Answers: 10,486 Site admin

    Yup, which continues to suggest that the problem is with your php script, or perhaps the database connection or similar. It isnt a JavaScript DataTables issue. The debugging needs to be done at the server side. I'm surprised there is no error in the error log.

    Allan

  • lukasztomlukasztom Posts: 9Questions: 1Answers: 0

    The file action.php is working fine, but when I modified the file Inventory.php and class getLodgeList() and put only one: echo "test," I can see the response AJAX with title Test.
    so we should modify this part of the code:?

    public function getLodgeList(){

    $sqlQuery = "SELECT * FROM lodge ";
    if(!empty($_POST["search"]["value"])){
    
        $sqlQuery .= 'WHERE no LIKE "%'.$_POST["search"]["value"].'%" ';
        $sqlQuery .= 'OR name LIKE "%'.$_POST["search"]["value"].'%" ';
        $sqlQuery .= 'OR address1 LIKE "%'.$_POST["search"]["value"].'%" ';
        $sqlQuery .= 'OR address2 LIKE "%'.$_POST["search"]["value"].'%" ';
        $sqlQuery .= 'OR town LIKE "%'.$_POST["search"]["value"].'%" ';
        $sqlQuery .= 'OR post_code LIKE "%'.$_POST["search"]["value"].'%" ';
        $sqlQuery .= 'OR province LIKE "%'.$_POST["search"]["value"].'%" ';
        $sqlQuery .= 'OR country LIKE "%'.$_POST["search"]["value"].'%" ';
        $sqlQuery .= 'OR mon LIKE "%'.$_POST["search"]["value"].'%" ';
        $sqlQuery .= 'OR tue LIKE "%'.$_POST["search"]["value"].'%" ';
        $sqlQuery .= 'OR wed LIKE "%'.$_POST["search"]["value"].'%" ';
        $sqlQuery .= 'OR thu LIKE "%'.$_POST["search"]["value"].'%" ';
        $sqlQuery .= 'OR fri LIKE "%'.$_POST["search"]["value"].'%" ';
        $sqlQuery .= 'OR sat LIKE "%'.$_POST["search"]["value"].'%" ';
        $sqlQuery .= 'OR sun LIKE "%'.$_POST["search"]["value"].'%" ';
        $sqlQuery .= 'OR notes LIKE "%'.$_POST["search"]["value"].'%" ';
    
    }
    
    if(!empty($_POST["order"])){
        $sqlQuery .= 'ORDER BY '.$_POST['order']['0']['column'].' '.$_POST['order']['0']['dir'].' ';
    } else {
        $sqlQuery .= 'ORDER BY province ASC ';
    }
    
    $result = mysqli_query($this->dbConnect, $sqlQuery);
    $numRows = mysqli_num_rows($result);
    $customerData = array();   
    while( $customer = mysqli_fetch_assoc($result) ) {     
        $customerRows = array();
        $customerRows[] = $customer['lodgeid'];
        $customerRows[] = $customer['no'];
        $customerRows[] = $customer['name'];
        $customerRows[] = $customer['address1'];
        $customerRows[] = $customer['address2'];
        $customerRows[] = $customer['town'];
        $customerRows[] = $customer['post_code'];
        $customerRows[] = $customer['province'];
        $customerRows[] = $customer['country'];        
        $customerRows[] = $customer['mon'];
        $customerRows[] = $customer['tue'];
        $customerRows[] = $customer['wed'];
        $customerRows[] = $customer['thu'];
        $customerRows[] = $customer['fri'];
        $customerRows[] = $customer['sat'];
        $customerRows[] = $customer['sun'];
        $customerRows[] = $customer['notes'];  
        //$customerRows[] = '<button type="button" name="update" id="'.$customer["lodgeid"].'" class="btn btn-primary btn-sm rounded-0 update" title="update"><i class="fa fa-edit"></i></button>';
        $customerRows[] = '';
        $customerData[] = $customerRows;
    }
    
    $output = array(
        "draw"              =>  intval($_POST["draw"]),
        "recordsTotal"      =>  $numRows,
        "recordsFiltered"   =>  $numRows,
        "data"              =>  $customerData
    );
    

    echo json_encode($output);

    }

  • lukasztomlukasztom Posts: 9Questions: 1Answers: 0

    In class getLodgeList() I changed :

    echo json_encode($output)

    to

    var_dump($output)

    so can see a response with records from the database (attached picture)

    so maybe we should modification this part:

    $output = array(
    "draw" => intval($_POST["draw"]),
    "recordsTotal" => $numRows,
    "recordsFiltered" => $numRows,
    "data" => $customerData
    );

    to get this work?

    Thanks
    Lukasz

  • kthorngrenkthorngren Posts: 21,391Questions: 26Answers: 4,961

    Maybe this article will help you debug why json_encode seems to be returning an empty result.

    Kevin

  • lukasztomlukasztom Posts: 9Questions: 1Answers: 0

    Thank you for the articel. Just did first step and added first code:

    echo json_last_error_msg(); // Print out the error if any
    die(); // halt the script

    and yes i got a message :

    "Malformed UTF-8 characters, possibly incorrectly encoded"

    so I have to modify this bit:

    $output = array(
    "draw" => intval($_POST["draw"]),
    "recordsTotal" => $numRows,
    "recordsFiltered" => $numRows,
    "data" => $customerData
    );

    echo json_encode($output);

    so instead I used this one :

    $output = array(
    "draw" => intval($_POST["draw"]),
    "recordsTotal" => $numRows,
    "recordsFiltered" => $numRows,
    "data" => $customerData
    );

    echo json_encode($output, JSON_UNESCAPED_UNICODE);

    but still, I am getting this error :

    "Malformed UTF-8 characters, possibly incorrectly encoded"

    Did I modify it wrong?

    Lukasz

  • kthorngrenkthorngren Posts: 21,391Questions: 26Answers: 4,961
    Answer βœ“

    I suspect you will need to dig into why this error is occurring. Guessing something with the server transfer has caused the issue. Maybe a DB version change or data migration issue. Its hard to say without knowing what was involved.

    This is not a Datatable issue and is beyond the scope of this forum. You will find more resources for your error on forums like Stack Overflow. Doing a Google search of the error and json_encode results in lots of Stack Overflow and other threads like this.

    Kein

  • lukasztomlukasztom Posts: 9Questions: 1Answers: 0

    Thank you for the article link:

    I updated to :

    echo json_encode($output, JSON_INVALID_UTF8_IGNORE);

    and is working :)
    Many thanks to everyone to helping me

    Thanks

  • kthorngrenkthorngren Posts: 21,391Questions: 26Answers: 4,961

    Glad you got it worked out!

    Kevin

Sign In or Register to comment.