How to correctly parse the response of an ajax request?

0

I am filling in a datatable but I would like to measure the length of the array that the query returns to me. In addition to this choose which elements of array I want to show and which do not.

The code I use is the following

var data2 = JSON.stringify({
                usuario: $('#h_user').val(),
                estado: $('#MainContent_estado option:selected').text(),
                buscarPor: $('#MainContent_buscarPor option:selected').text(),
                fecha_ini: $('#MainContent_fechaIni').val(),
                fecha_fin: $('#MainContent_fechaFin').val()
            })

         $('#datatable').DataTable({
                        'dom': 'Bfrtip',
                        'buttons': [
                            {
                                extend: 'excelHtml5', className: 'btn btn-primary', exportOptions: {
                                    columns: ':visible'
                                }
                            },
                            {
                                extend: 'pdfHtml5', className: 'btn btn-primary', exportOptions: {
                                    columns: ':visible'
                                }
                            },
                            {
                                extend: 'colvis', className: 'btn btn-primary', text: 'Columnas Visibles'
                            }
                            //'colvis'
                        ],
                        "columnDefs": [ {
                            "targets": -1,
                            "visible": false
                        } ],
                        "autoWidth": true,
                        "ajax": {                
                            "url": "http://vr.zom.com/extranete/WSo/Servicioss.svc/buscaReferes",
                            "type": "POST",
                            "contentType": "application/json; charset=utf-8",
                            "data": function (d) {

                                return data2


                            },


                            "error": function (jqXmlHttpRequest, textStatus, errorThrown) {
                               // alert("Datos Incorrectos"); //#coment
                            }

                        }
                    });

and if it works the datatable is filled with the data but now I want to filter which dobjects of the array show

    
asked by emanuelle 04.05.2018 в 17:31
source

1 answer

2

What you are looking for is the dataSrc property. It is used like this:

"ajax": {                
            "url": "http://ver.moz.com.mx/extranet/WS/Servicios.svc/buscaRefer",
            "type": "POST",
            "contentType": "application/json; charset=utf-8",
            "data": function (d) {
                        return data2
            },
            "dataSrc": function ( json ) {
                         console.log(json.data.length);
                         return json.data;
            }
        }
    
answered by 04.05.2018 в 18:43