AJAX sometimes fails

AJAX sometimes fails

FedericoVFedericoV Posts: 35Questions: 9Answers: 0

I'm experiencing a weird issue using AJAX in my Datatables...sometimes, the call to the remote script fails and I get the following error:
DataTables warning: table id = table - Ajax error. For more information about this error, please see http://datatables.net/tn/7.
The above error seems occure when the users types into the search form too fast...so the server doesn't provide the answer in short time.
I'm not asking any hint or suggestions on server side sript, of course, I'm just wondering if there is any strategy on Datatables setting to avoid such error.

Below my code, and thanks for any suggestion:

     table.DataTable({
        dom: 'lrtip',
        responsive: true,
        autoWidth: false,
        searchDelay: 500,
        processing: true,
        serverSide: true,
        stateSave: true,
        lengthMenu: [[15, 25, 50, -1], [15, 25, 50, "All"]],
        searchHighlight: true,
        ajax: {
            url: "<?php echo site_url('rmidev/domestic_list')?>",
            type: "POST",
            "data": function ( d ) {
                d.csrf_test_name = token_hash;
            },
            "dataSrc": function ( json ) {
                token_hash = json['csrf_test_name'];
                for(var i=0; i< json.data.length; i++){
                    json.data[i].mm = '<a href="' + controller_url + '/data/' + json.data[i].id + '">' + json.data[i].mm + '</a>';
                }
                //return return_data;
                return json.data;
            }
        },
        order: [[2, 'asc']],
        deferRender: true,
        columns: [
            {data: 'mm'},
            {data: 'ditta'},
            {data: 'tipo'},
            {data: 'motori'},
            {data: 'nc'},
        ],
        columnDefs: [
            {
                targets: [ 3, 4 ],
                orderable: false,
                searchable: false,
            },
        ],
        "language": {"url": "<?php echo site_url('assets/public/plugins/datatables/lang/dataTables.') . $actual_lang . '.lang'; ?>"},
        "pagingType": "numbers",
    });

This question has an accepted answers - jump to answer

Answers

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

    Have you followed the link given in the error message?

  • FedericoVFedericoV Posts: 35Questions: 9Answers: 0

    Yep, Datatables show that error if it doesn't receive a 2xx HTTP status code ...as I wrote because the server didn't reply so fast as user typing other search values.
    Maybe I could get rid of the alert or show different error message, but don't know exctly how to use the following code:

    $.fn.dataTable.ext.errMode = 'throw';
    
  • kthorngrenkthorngren Posts: 21,311Questions: 26Answers: 4,948
    edited April 2020 Answer ✓

    If the problem is that the server script is too slow in responding maybe you can change the search input from using the keyup event to the change event, for example:
    http://live.datatables.net/jududavi/1/edit

    Or you can try the debounce technique described in this thread.

    Sorry I haven't used $.fn.dataTable.ext.errMode nor the error event to try capturing errors so won't be helpful there.

    Kevin

  • FedericoVFedericoV Posts: 35Questions: 9Answers: 0

    Really interesting the debounce technique...I'll give it a try.
    Thanks for invaluable help Kein...Datatable is a whole new world to explore

This discussion has been closed.