Errors When Reordering / searching / length

Errors When Reordering / searching / length

WebCodexWebCodex Posts: 71Questions: 13Answers: 3

Link to test case: N/A
Debugger code (debug.datatables.net): https://debug.datatables.net/aguneh
Error messages shown: DataTables warning: table id=datatable - Invalid JSON response. For more information about this error, please see http://datatables.net/tn/1
Description of problem: Intermittent errors when trying to reorder, search or set length. I updated everything but the debugger says rowReorder 1.2.7 is still on 1.2.6 :neutral:

The page is in an admin section so no way to send you there unfortunately but I'm using serverSide with the PHP script from this website with the complex function. Everywhere else on my site it is working fine. I can't see any errors in the code and the table is a very simple info table, nothing special about it at all.

Thanks in advance.

This question has an accepted answers - jump to answer

Answers

  • kthorngrenkthorngren Posts: 21,341Questions: 26Answers: 4,954

    The place to start troubleshooting is to use the steps provided at the link in the error:
    http://datatables.net/tn/1

    Let us know what you find.

    Kevin

  • WebCodexWebCodex Posts: 71Questions: 13Answers: 3

    I've been down this road so many times now, always some erroneous error, this one has me stumped, nothing in console, no obvious code issues, here take a look.

    The Datatable:

    var oldStart = 0;
    var getResources = true;
    
    var table = $('#datatable').dataTable({
        "ajax": {
            'type': 'POST',
            'dataType': 'json',
            'url': 'vcadmin/controllers/resourceControl.php',
            "data": function ( d ) {
                d.getResources = getResources; 
            },
        },
    
        drawCallback: function (o) {
            var newStart = this.api().page.info().start;
    
            if ( newStart != oldStart ) {
                var targetOffset = $('#datatable').offset().top;
                $('html,body').animate({scrollTop: targetOffset}, 500);
                oldStart = newStart;
            }
    
        },
        "language": {
            "processing": "Loading Resources",
        },
        "pagingType": "numbers",
        "displayStart": 0,
        "processing": true,
        "serverSide": true,
        "paging": true,
        //responsive: true,
        //stateSave: true,
        "stateDuration": -1,
        select: false,
        order: [[1, 'asc']],
        "deferRender": true,
        "lengthMenu": [[10, 25], [10, 25]],
        columnDefs: [
        { targets: 8, defaultContent: '', "searchable": false, orderable: false },
        ],
        "columns": [
            { "searchable": false, "visible": false  },
            null,
            null,
            null,
            null,
            null,
            null,
            null,
            { 
                "render": function ( data, type, row, meta ) {
                    return '<a href="vcadmin/editresource/'+row[0]+'" title="Edit This Resource" class="editresource btn btn-primary">Edit</a>';
                }, 
            },
        ]
    });
    

    **The PHP Controller: **

    Complex is the same complex as found in the SSP here on the website https://datatables.net/download/packages

    /**
        GET RESOURCES
    */
    
    if(isset($_POST['getResources'])) {
        if($_SERVER['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest'){
    
            $primaryKey = 'resource_id';
            $table = 'resources';
    
            $columns = array(
                array( 'db' => 'resource_id', 'dt' => 0 ),
                array( 'db' => 'resource_name', 'dt' => 1 ),
                array( 'db' => 'resource_url', 'dt' => 2 ),
                array( 'db' => 'resource_description', 'dt' => 3 ),
                array( 'db' => 'resource_coupon', 'dt' => 4 ),
                array( 'db' => 'resource_coupon_amount', 'dt' => 5 ),
                array( 'db' => 'resource_category', 'dt' => 6 ),
                array( 'db' => 'resource_country', 'dt' => 7 )
            );
    
            echo json_encode($dttables->complex ( $_POST, $table, $primaryKey, $columns ));
    
        }else {
            echo 'Invalid Token';
        }
    }
    
  • WebCodexWebCodex Posts: 71Questions: 13Answers: 3
    edited December 2020

    How does dataTables deal with special characters, for example: parentheses, single and double quotes etc, do I need to escape them before they reach the Table?

    I'm thinking that is the error I'm getting

  • kthorngrenkthorngren Posts: 21,341Questions: 26Answers: 4,954
    Answer ✓

    What did you find, using the browser's network inspector, as a response when you get the Invalid JSON response error?

    How does dataTables deal with special characters, for example: parentheses, single and double quotes etc, do I need to escape them before they reach the Table?

    You shouldn't need to do anything with them. The Invalid JSON response error is before Datatables tries to populate the table.

    Kevin

  • WebCodexWebCodex Posts: 71Questions: 13Answers: 3

    I found that a single quote in a string was preventing the table from loading, either that or this (string), I deleted the whole string from the DB and all works fine now.

This discussion has been closed.