2

I am trying to list data from a table using datatables but I am getting the error

"Uncaught TypeError: Cannot read property 'length' of undefined"

And I dont know why. I appreciate any help as I am new in this kind of programs.

My html:

<table class="table mg-b-0 table-contact" style="width:100% !important;" id="preEnrolTable">
            <thead>
            <tr>
                <!--<th class="wd-5p">
                    <label class="ckbox mg-b-0">
                        <input type="checkbox"><span></span>
                    </label>
                </th>-->
                <th class="tx-10-force tx-mont tx-medium">DNI</th>
                <th class="tx-10-force tx-mont tx-medium hidden-xs-down">Nombre</th>
                <th class="tx-10-force tx-mont tx-medium hidden-xs-down">Tarjeta</th>
                <th class="tx-10-force tx-mont tx-medium hidden-xs-down">Clave</th>
                <th class="tx-10-force tx-mont tx-medium hidden-xs-down">Privilegio</th>
                <th class="tx-10-force tx-mont tx-medium hidden-xs-down">Huella</th>
                <th class="tx-10-force tx-mont tx-medium hidden-xs-down">Rostro</th>
                <th class="wd-5p hidden-xs-down"></th>
            </tr>
            </thead>
        </table>

This is the script that should bring the data:

function getPreEnrol() {

            $("#preEnrolTable").DataTable().clear();
            $("#preEnrolTable").DataTable().destroy();
            var table = jQuery("#preEnrolTable");
            var oTable = table
                .on('preXhr.dt',function(e,settins,data){
                    $("#modalLoadText").html('Estamos cargando la información, espere un momento!');
                    $("#modalLoad").modal('show');
                })

                .dataTable({
                    "processing": false,
                    "autoWidth": false,
                    "paging": true,
                    "pageLength": 5,
                    "serverSide": true,
                    "order": [
                        [0, 'asc']
                    ],
                    "ajax": {
                        "url": "SYS_include/WS/PreEnrolamiento/WS_PreEnrolamiento.php",
                        "data": {
                            "getDatos":true
                        }
                    }
...

And this is the script that is being called in the ajax url:

$Permisos = new Permisos();
$pagina=45;
$modulo=3;
$i = $Permisos->traerPermisos($json,$pagina,$modulo);

$preEnrol = new PreEnrolamiento();

//error_log($_GET["getDatos"]); // devuelve true
//error_log(print_r($_GET["getDatos"])); // devuelve 1
if(isset($_GET["getDatos"]) && !empty($_GET["getDatos"])){

    switch ($i) {
        case 1:
        case 2:
        case 3:
        case 4:
        case 5:
            echo $preEnrol->getPreEnrolamientos($instancia,$sucursal,$conn);
            break;
    }
}else if(isset($_POST["getNac"]) && !empty($_POST["getNac"])){

    switch ($i) {
        case 1:
        case 2:
        case 3:
        case 4:
        case 5:
            $nacionalidades = Dni::obtenerNacionalidades($instancia,$conn);
            echo json_encode($nacionalidades);
            break;
    }

}
Gyrocode.com
  • 57,606
  • 14
  • 150
  • 185
  • 1
    View the ajax response in your browser's network inspector to make sure it is what is expected. Most likely it is not. – Patrick Q Jul 30 '19 at 20:24
  • Also, are you sure that `$("#preEnrolTable")` shouldn't be `$("#ColabsTable")`? I don't see an element with id "preEnrolTable" in the HTML above. – Patrick Q Jul 30 '19 at 20:26
  • Thank you, one problem solve, indeed the id was wrong,butim still getting the same error, i check the console network response and im getting a long response, a lot of data, impossible to show here unfortunaly,what else can i check? – Jorge Avalos Jul 30 '19 at 21:14
  • 1
    Is the data what you _expect_? Is it a response that can be parsed by datatables? – Patrick Q Jul 30 '19 at 21:19
  • yes it is what i expect :( The module stay loading and the console still givme the Uncaught TypeError: Cannot read property 'length' of undefined – Jorge Avalos Jul 30 '19 at 21:23
  • 1
    Which line does it say the error is on? Because you're not calling `.length` in the code above. – Patrick Q Jul 30 '19 at 21:26
  • that error is shown in the jquery.dataTables.js, in this line for ( var i=0, ien=data.length ; i – Jorge Avalos Jul 30 '19 at 21:31
  • That's what I suspected. That means that your ajax response is _not_ something parse-able by datatables – Patrick Q Jul 31 '19 at 11:59
  • 1
    Possible duplicate of [DataTables: Cannot read property 'length' of undefined](https://stackoverflow.com/questions/34287402/datatables-cannot-read-property-length-of-undefined) – Gyrocode.com Jul 31 '19 at 12:01
  • i will check then, thank you – Jorge Avalos Aug 01 '19 at 13:25

1 Answers1

0

Finally i found the solution to my problem, i will post it here if anyone need it, my ajax call:

"ajax": {

                    "url": "SYS_include/WS/PreEnrolamiento/WS_PreEnrolamiento.php", 
                    "type":"get", //added
                    "dataType":'json', //added                  
                    "dataSrc": "", //added
                    "data":{"getDatos": true }
                      },