Datatable with JSON

2

I'm doing a datatable with ajax return.

$.getJSON("sql.php?operacao=usuario_lista&excluidos=false", function (data) {
    alert(data.users);

    $('#tabusuarios').DataTable({
        "aaData": data.users,
        "aoColumns": [
            { "mDataProp": "ID" },
            { "mDataProp": "NOME" },
            { "mDataProp": "USUARIO" }
        ]
    });
});

My JSON returns:

{
    "users": [
        {
            "ID": "89",
            "NOME": "aaabbbb",
            "USUARIO": "a.b",
            "DTNASC": "2017-01-01",
            "NUM": "112233"
        },
        {
            "ID": "76",
            "NOME": "Bruno Pereira",
            "USUARIO": "brunopereira",
            "DTNASC": "2011-01-01",
            "NUM": "11"
        }
    ]
}

But he gives the error:

  

DataTables warning: table id = tabusers - Can not reinitialise   DataTable. For more information about this error, please see    link

And do not populate the table ..

    
asked by anonymous 25.01.2017 / 19:49

1 answer

2

As answer in SOen , you can do as follows:

$(document).ready(function() {
    $('#tabusuarios').DataTable({
        "processing" : true,
        "ajax" : {
            "url" : "sql.php?operacao=usuario_lista&excluidos=false",
            dataSrc : ''
        },
        "columns" : [ {
            "data" : "ID"
        }, {
            "data" : "NOME"
        }, {
            "data" : "USUARIO"
        }]
    });
});
    
25.01.2017 / 20:55