Laravel-datatables: TokenMismatchException when using POST

Created on 6 Oct 2015  路  4Comments  路  Source: yajra/laravel-datatables

Hi,
I have a datatable with a lot of fields, and it was causing an error because the URL was too long.

A post over at datatables.net said to use POST instead of GET.

I changed my js as follows:

 $('#users-table').DataTable({
        processing: true,
        serverSide: true,
        ajax: {
           "url":  '{!! route('datatables.data') !!}',
           "type": "POST"
        },
        "lengthMenu": [[10, 25, 50, -1], [10, 25, 50, "All"]],
        columns: [
            { data: 'id', name: 'id'},
            { data: 'AccountDate', name: 'AccountDate'},
            { data: 'Cust', name: 'Cust'},
            { data: 'CustType', name: 'CustType'},
            ...etc...

Now I'm getting a 500 internal server error:
TokenMismatchException in VerifyCsrfToken.php line 53:

Any thoughts on how to get around that?

Most helpful comment

Im not sure if this will help you but this will work also for including the token along with your ajax request

                          $('#users-table').DataTable({
                                processing: true,
                                serverSide: true,
                                ajax: {
                                        'url': '{{ route('user.table') }}',
                                        'type': 'POST',
                                        'headers': {
                                            'X-CSRF-TOKEN': '{{ csrf_token() }}'
                                        }
                                },
                                columns: [
                                    { data: 'name', name: 'name' },
                                    ......
                                ]
                            });

Laravel Documentation:
http://laravel.com/docs/master/routing#csrf-x-csrf-token

All 4 comments

Ok found the answer to the CSRF issue:
http://laravel.com/docs/5.1/routing#csrf-excluding-uris

Edit app\Http\Middleware\VerifyCsrfToken.php\:

class VerifyCsrfToken extends BaseVerifier
{
    /**
     * The URIs that should be excluded from CSRF verification.
     *
     * @var array
     */
    protected $except = [
        //
        'datatables/*',
    ];
}

Im not sure if this will help you but this will work also for including the token along with your ajax request

                          $('#users-table').DataTable({
                                processing: true,
                                serverSide: true,
                                ajax: {
                                        'url': '{{ route('user.table') }}',
                                        'type': 'POST',
                                        'headers': {
                                            'X-CSRF-TOKEN': '{{ csrf_token() }}'
                                        }
                                },
                                columns: [
                                    { data: 'name', name: 'name' },
                                    ......
                                ]
                            });

Laravel Documentation:
http://laravel.com/docs/master/routing#csrf-x-csrf-token

mastrip2, I haven't tried this but LOVE IT - I will give it a try!

@mastrip2 Just nailed it; changed my initialization as follow.

var tableIndex=$('#tableIndex').DataTable({
"order": [[ 0, "asc" ]],
"aLengthMenu": [[5, 10, 25], [5, 10, 25]],
"iDisplayLength": 10,
"dateFormat": 'yyyy-mm-dd',
"processing": true,
"serverSide": true,
"ajax": {
'url': '{{route("clinics.api.laboratoryIndex")}}',
'type': 'POST',
'headers': {
'X-CSRF-TOKEN': '{{ csrf_token() }}'
}
},

"columns":[
{data: 'products', name: 'products'},
],
"columnDefs": [
{ "searchable": false, "bSortable": false, "targets": 0 }
],
language: {
"sProcessing": "Procesando...",
"sLengthMenu": "Mostrar _MENU_ registros",
"sZeroRecords": "No se encontraron resultados",
"sEmptyTable": "Ning煤n dato disponible en esta tabla",
"sInfo": "Mostrando registros del _START_ al _END_ de un total de _TOTAL_ registros",
"sInfoEmpty": "Mostrando registros del 0 al 0 de un total de 0 registros",
"sInfoFiltered": "(filtrado de un total de _MAX_ registros)",
"sInfoPostFix": "",
"sSearch": "Buscar: ",
"sUrl": "",
"sInfoThousands": ",",
"sLoadingRecords": "Cargando...",
"oPaginate": {
"sFirst": "Primero",
"sLast": "脷ltimo",
"sNext": "Siguiente",
"sPrevious": "Anterior"
},
"oAria": {
"sSortAscending": ": Activar para ordenar la columna de manera ascendente",
"sSortDescending": ": Activar para ordenar la columna de manera descendente"
}
}
});

Thank you fam

Was this page helpful?
0 / 5 - 0 ratings

Related issues

baig772 picture baig772  路  14Comments

ezekel picture ezekel  路  17Comments

jay-shah-rushiinfotech picture jay-shah-rushiinfotech  路  19Comments

mithleshjs picture mithleshjs  路  16Comments

ZAZmaster picture ZAZmaster  路  15Comments