I'm doing a query by ajax
implementing laravel
and jquery
but my problem is that it is not showing me the data it brings me, and in the console it tells me this error Uncaught TypeError: Cannot read property 'length' of undefined
but I look at the network and if it is bringing me the data, the problem is in the javascript that is not capturing me, I appreciate your help
I leave javascript code and driver
<title>Lista de Granjas</title>
<input type="hidden" name="_token" value="{{csrf_token()}} ">
<div class="panel panel-danger">
<div class="panel-heading" id="titulo">
<h4><i class="fa fa-list" aria-hidden="true"></i> Lista de Granjas</h4>
</div>
<br>
<div class="form-group container-fluid">
<div class="form-group pull-right">
<a href="javascript:history.go(-1);" class="btn btn-info"><i class="fa fa-arrow-left" aria-hidden="true"></i> Regresar</a>
</div>
</div>
<div class="panel-body table-responsive">
<table id="list_granjas" class="table table-bordered table-hover text-center" cellspacing="0" width="100%">
<thead>
<tr style="color: white">
<th>Nombre de la Granja</th>
<th>Descripcion</th>
<th>Direccion</th>
<th>Numero de Contacto</th>
<th>Porcentaje de Precebo</th>
<th>Porcentaje de Ceba</th>
</tr>
</thead>
<tbody>
</tbody>
</table>
</div>
</div>
<script type="text/javascript">
$(document).ready(function () {
var token = $("[name='_token']").val();
$('#list_granjas').DataTable( {
"processing": true,
"serverSide": true,
"ajax": {
"url": "/list",
"type": "POST",
"data":{ _token: "{{csrf_token()}}"}
},
"columns": [
{ data: 'nombre_granja'},
{ data: 'descripcion_granja'},
{ data: 'direccion_granja'},
{ data: 'numero_contacto_granja'},
{ data: 'porcentaje_precebo'},
{ data: 'porcentaje_ceba'}
]
});
})
</script>
driver function
public function listGranjas()
{
$granjas = Granja::all();
return response()->json(
$granjas->toArray()
);
}