Datatables jquery ajax error canceled

1

I'm doing a catalog of kardex of the employee where I have 2 instances of the datatables plugin jquery :

The first request is done correctly but the second one sends an error ajax canceled .

I am not very clear why this error is coming out, apparently the first request takes and cancels the second or I do not know if it is a datatables issue.

What I have achieved so far is to put a delay: 500 in the request but this solution I do not like

any advice?

Post in English is apparently the same problem

    
asked by Gussjq 06.01.2016 в 17:57
source

4 answers

3

One way to control the possible errors when receiving the data via AJAX, is using 2 functions.

Example:

getAllUsers function: In charge of calling the data.

ShowDataDataTable function: In charge of filling the datatable and being able to visualize the data.

Jquery DataTables version: 1.10.7

//Global variables
var oTableTR;

//función que muestra los datos en el datatable
function mostrarDatosDataTable(result) {
    try {
        oTableTR = $('#listaDatosDt').DataTable({
            "aLengthMenu": [[15, 30, 60, 120, -1], [15, 30, 60, 120, "All"]],
            "bProcessing": true,
            "scrollX": true,
            "aaData": result,
            "aoColumns": [
                { "mDataProp": "usuarioId" }, { "mDataProp": "usuarioNombre" }, { "mDataProp": "usuarioDocumento" }, { "mDataProp": "usuarioEstado" }
            ],
            "aaSorting": [[0, "desc"]]
        });
    } catch (exception) {
        message("Error", "Attention", "error");
    }
    return false;
};

//función que llama los datos
var getAllUsers = function () {
    var datae = { 'param': 'param' };
    $.ajax({
        type: "POST",
        url: "WebServices/Usuarios.asmx/GetAllUsers",
        contentType: "application/json; charset=utf-8",
        data: JSON.stringify(datae),
        dataType: "json",
        success: function (result) {
            var data = result.d;
            try {
                var dataJson = $.parseJSON(data);
                if (dataJson.status === "ok") {
                    oTableTR = $('#listaDatosDt').DataTable();
                    oTableTR.destroy();
                    try {
                        mostrarDatosDataTable(dataJson.data);
                    } catch (exception) {
                        message("Error", "Attention", "error");
                    }


                } else {
                    if (dataJson.data === "NoSession") {
                        document.location.href = "index.html?BackPageSession=NoSession";
                    } else {
                        message("Error", "Attention", "error");
                    }
                }
            } catch (e) {
                message("Error", "Attention", "error");
            }

        },
        error: function (error) {
            message("Error", "Attention", "error");
        }

    });
    return false;
}

//llamado función
getAllUsers();
    
answered by 20.01.2016 в 20:18
1

Maybe, if your problem is the simultaneous execution of the different pluging instances with their respective ajax calls, I would try to configure the ajax in a non-asynchronous way (async: false). I do not know if this option is possible in pluging, but it is an ajax property so it should work. Good luck!

    
answered by 09.01.2016 в 20:53
1

you can not start the table again, what you can do is clean the table and repopulate it with records.

// instancias la tabla
if ( ! $.fn.DataTable.isDataTable('#you_table') ) {
    oTable = $('#you_table').dataTable(options);
}else{
    oTable = $('#you_table').dataTable();
}

// Limpiar registros
oTable.fnClearTable();

// Poblar la tabla - pueder usar for(){} de javascript o jquery
// yourData representa tu arreglo de kardex del empleado
$.each( yourData , function(index, valor){
   oTable.fnAddData(valor);
});
    
answered by 20.05.2016 в 00:34
0

You can not reinitialize a table that has already been initialized by DataTable . For this you must check if the table has been initialized with this function:

if ( ! $.fn.DataTable.isDataTable( '#example' ) ) {
  //Inicializar dataTable.
  $('#example').dataTable();
}

With this function, you should not cancel the second request that is shown on your image, and move correctly with the other requests. Here is the documentation: Doc

    
answered by 06.01.2016 в 18:06