3 votes

Change the Method of a JQuery Datatable from GET to POST in Laravel

The problem that I submit using GET at the Table is that when you exceed a certain number of columns jumps an error of ajax and does not allow to paginate and to do searches:

DataTables warning: table id=TablaDatos - Ajax error. For more information about this error, please see http://datatables.net/tn/7

I was reading several articles that say that this occurs because it exceeds the number of characters allowed for GET and the solution is to change it by POST but I have tried several ways and I cannot implement it with POST

My view is the Following:

<div class="tab-pane active" id="camaras">
  <!-- Tabla de Datos -->
  <div class="box-body">  
    <div class="row">  
      <div class="col-lg-12 col-md-12 col-sm-12 col-xs-12">
        <div class="table-responsive">
          <table id="TablaDatos" class="table table-striped table-bordered table-condensed table-hover dataTable dt-responsive nowrap" style="width:100%">
            <thead>
              <th>Código de la Cámara</th>
              <th>Descripción de la Cámara</th>
              <th>Email</th>
              <th>Razón Social</th>
              <th>Tipo</th>
              <th>Sector</th>
              <th>Estado</th>
              <th>Municipio</th>
              <th>Parroquia</th>
              <th>Dirección</th>
              <th>Cédula</th>
              <th>Nombre</th>
              <th>Apellido</th>
              <th>Email</th>
              <th>Cargo</th>
              <th>Acciones</th>
            </thead>                    
          </table>
        </div>
      </div>  
    </div>
  </div>
  <!-- /.Tabla de Datos -->
</div> 

The Code Javascript for the Datatable:

var table = $('#TablaDatos').DataTable({
        language:
        {
          url: "{{asset('bower_components/datatables.net-bs/spanish.json')}}"
        },
        paging    : false,
        ordering  : true,
        processing: true,
        serverSide: true,
        scrollY   : '25vh',
        scrollCollapse: true,
        columnDefs: 
        [ 
            {orderable: false,searchable: false}
        ],
        order: [[ 1, 'asc' ]],
        rowId     : 'CAM_CODIGO',
        dom       : "<'row'<'col-sm-4'B><'col-sm-4'><'col-sm-4'f>>" +
                    "<'row'<'col-sm-12'tr>>" +
                    "<'row'<'col-sm-5'i><'col-sm-7'p>>",
        buttons   :
        [
            {extend: "copy",className: "btn-sm"},
            {extend: "csv",className: "btn-sm"},
            {extend: "excelHtml5",className: "btn-sm"},
            {extend: "pdfHtml5",className: "btn-sm"},
            {extend: "print",className: "btn-sm"},
            {text: 'Refresh',
                action: function () {
                    table.ajax.reload();
                },
                className: "btn-sm"
            },
    ],
    responsive : true,
        ajax      :
        {
            url   :"{{route('buscador.getdata')}}",
            type  :"GET"
        },
        deferRender: true,

        columns   :
        [
            {data: 'CAM_CODIGO', name:'CAM_CODIGO', id:'CAM_CODIGO', className: 'sel'},
            {data:'CAM_ALIAS', className: 'sel'},
            {data:'CAM_EMAIL' , className: 'sel'},
            {data:'CAM_DESCRI', className: 'sel'},
            {data:'TIP_DESCRI', className: 'sel'},
            {data:'SEC_DESCRI', className: 'sel'},
            {data:'EDO_DESCRI', className: 'sel'},
            {data:'MUN_DESCRI', className: 'sel'},
            {data:'PAR_DESCRI', className: 'sel'},
            {data:'CAM_DIRECC', className: 'sel'},
            {data:'CAM_CEDREP', className: 'sel'},
            {data:'CAM_NOMREP', className: 'sel'},
            {data:'CAM_APEREP', className: 'sel'},
            {data:'CAM_EMAREP', className: 'sel'},
            {data:'CAR_DESCRI', className: 'sel'},
            {data: 'action', name: 'action', orderable: false, searchable: false, className: 'sel' }
        ],
    });

The Driver:

public function getdata()
{
    $camara = DB::table('dpcamaras');
    return Datatables::of($camara)
            ->addColumn('action', function ($camara)
            {
                return '<a href="#" class="btn btn-info fa fa-pencil editcam" id="'.$camara->CAM_CODIGO.'"></a>
                        <a href="#" class="btn btn-danger fa fa-close delcam" id="'.$camara->CAM_CODIGO.'"></a>';
            })
            ->make(true);

}

The Route:

Route::get('camara/buscador/getdata','BuscadorController@getdata')->name('buscador.getdata');

My question is how to I migrate this DataTable from GET to POST and do not skip error for the number of columns. With 4 to 5 runs but more 5 it gives error with AJAX.

Do you any Suggestion to help me solve the problem?

1voto

After so much searching, I managed to find the solution to the error. To send the POST method, the generated Token had to be sent with the data so that AJAX could process the query of the Table

 ajax      :
{
    url   :"{{route('buscador.getdata')}}",
    type  :"POST",
    data  : {_token: "{{csrf_token()}}"}
},
 

Thank you very much everyone for the help provided.

0voto

Modify the Controller in this way

 public function getdata(Request $request)
{
    $camara = DB::table('dpcamaras');
    return Datatables::of($camara)
            ->addColumn('action', function ($camara)
            {
                return '<a href="#" class="btn btn-info fa fa-pencil editcam" id="'.$camara->CAM_CODIGO.'"></a>
                        <a href="#" class="btn btn-danger fa fa-close delcam" id="'.$camara->CAM_CODIGO.'"></a>';
            })
            ->make(true);

}
 

and the Route:

 Route::post('camara/buscador/getdata','BuscadorController@getdata')->name('buscador.getdata');
 

But I still do not solve the Problem. Da Error 419 unknown status

 {message: "", exception: "Symfony\Component\HttpKernel\Exception\HttpException",…}
exception: "Symfony\Component\HttpKernel\Exception\HttpException"
file: "vendor\laravel\framework\src\Illuminate\Foundation\Exceptions\Handler.php"
line: 203
message: ""
trace: [{,…},…]
 

HolaDevs.com

HolaDevs is an online community of programmers and software lovers.
You can check other people responses or create a new question if you don't find a solution

Powered by:

X