Show data jQuery

Show data jQuery

Danillo Leão LopesDanillo Leão Lopes Posts: 3Questions: 2Answers: 0
edited May 2018 in Free community support

I'm try to show data but i have errors

[{
    "id": 5,
    "nome_fantasia": "Danillo Leão Lopes",
    "cnpj_cpf": "600.520.763-69",
    "telefone": "(99) 99102-6245",
    "pessoa": 1,
    "contribuinte": 9
}, {
    "id": 6,
    "nome_fantasia": "Samuel Fernandes Leão",
    "cnpj_cpf": "000.000.000-00",
    "telefone": "(00) 00000-0000",
    "pessoa": 1,
    "contribuinte": 9
}, {
    "id": 7,
    "nome_fantasia": "Luciane Leão Lopes",
    "cnpj_cpf": "000.000.000-00",
    "telefone": "(00) 00000-0000",
    "pessoa": 1,
    "contribuinte": 9
}, {
    "id": 8,
    "nome_fantasia": "Raimundo Lopes Dias Filho",
    "cnpj_cpf": "000.000.000-00",
    "telefone": "(00) 00000-0000",
    "pessoa": 1,
    "contribuinte": 9
}, {
    "id": 9,
    "nome_fantasia": "Alisson Leão Lopes",
    "cnpj_cpf": "000.000.000-00",
    "telefone": "(00) 00000-0000",
    "pessoa": 1,
    "contribuinte": 9
}, {
    "id": 10,
    "nome_fantasia": "Bruno Leão Lopes",
    "cnpj_cpf": "000.000.000-00",
    "telefone": "(00) 00000-0000",
    "pessoa": 1,
    "contribuinte": 9
}]

$.ajax({
        url: '/pesquisa/registros/clientes',
        data: {
            'registros':registros
        },
        dataType:'json',
        success: function(data){
            $('#clts').DataTable({
                processing:true,
                serverSide:true,
                data: data,
                columns: [
                    { title: "#" },
                    { title: "Nome" },
                    { title: "CNPJ" },
                    { title: "Telefone" },
                    { title: "#" },
                    { title: "#" }
                ],
            })
        },
        error: function(error){
            alert(JSON.stringify(error))
        }
    })

This question has an accepted answers - jump to answer

Answers

  • kthorngrenkthorngren Posts: 21,117Questions: 26Answers: 4,916
    Answer ✓

    I see three issues:

    1. The JSON format does not match the format required by server side processing. The the Server Side Processing doc for details.
    2. You are using ajax ouside of Datatables. According to the same doc page you need to use the Datatables ajax for server side processing.
    3. Your JSON response is structured as an array of objects. You will need to configure columns.data to match the structure of your data. Also review the Data docs for more info.

    If you don't need server side processing enabled (serverSide: true,) then your above code may work with just fixing number 3.

    Kevin

This discussion has been closed.