laravel-datatables: 401 Unauthorized

I updated today to 5.11.7 version and I am having some randomly unauthorized error from the ajax when I enter to the page where I have Datatables component. My code is like this

    public function anyData()
    {
      $Patient = Patient::select('*')->withTrashed();
      return \Datatables::of($Patient)->make(true);
    }

and the datatable declaration is

$(document).ready(function() {
    $('#dataTables-patients').DataTable({
      responsive: true,
      processing: true,
      serverSide: true,
      ajax: base_url+'/patient/data',
      language:{
        url:base_url+'/js/dataTables.Spanish.json'
      },
      columns:[
        {data:'dni', name:'dni'},
        {data:'first_name', name:'first_name'},
        {data:'last_name', name:'last_name'},
        {data:'birthday', name:'birthday'},
        {data:'id', name:'id'},
        {data:'active', name:'active'},
      ],
      "columnDefs": [{
        "targets": 4,
        "data": "id",
        "render": function ( data, type, row ) {
          if(row.active=="0"){
            return '<a class="btn btn-primary" href="'+base_url+'/patient/show/'+data+'">'+patient.view+'</a>'+
                   '<a class="btn btn-success respatient" data-id="'+data+'" href="#" >'+patient.restore+'</a>';
          }else{
            return '<a class="btn btn-primary" href="'+base_url+'/patient/show/'+data+'">'+patient.view+'</a>'+
                   '<a class="btn btn-danger delpatient" data-id="'+data+'" href="#" >'+patient.delete+'</a>';
          }
        }

      },
      { "orderable": false, "targets": [ 4 ] },
      { "bSearchable": false, "aTargets": [ 5 ] },
      { "visible": false,  "targets": [ 5 ] }
      ]
    });

});

when Unauthorized explodes, I have to re login on the app. and it happens randomly: I can reload 6 times the page and happens nothing, and sometimes I enter two times and unauthorized happens.

About this issue

  • Original URL
  • State: closed
  • Created 9 years ago
  • Reactions: 4
  • Comments: 16 (5 by maintainers)

Most upvoted comments

Reading the comments, I solved the problem well. in the view var token = '{{ csrf_token() }}';

ajax: { url:'$url', type:'post', data: { '_token': token } },

`var table = $(‘#myTable’);

        oTable = table.dataTable({
            processing: true,
            serverSide: true,
            order: [[1, 'desc']],
            buttons: [
				{ extend: 'print', 	className: 'btn dark btn-outline' 		},
				{ extend: 'copy', 	className: 'btn red btn-outline' 		},
				{ extend: 'pdf', 	className: 'btn green btn-outline' 		},
				{ extend: 'excel', 	className: 'btn yellow btn-outline' 	},
				{ extend: 'csv', 	className: 'btn purple btn-outline' 	},
				{ extend: 'colvis', className: 'btn dark btn-outline', text: 'Columns'}
			],
            ajax: {
                url:'$url',
                type:'post',
                data: {
                    '_token': token
                }
            },
            $col,
            \"oLanguage\": {
				\"sEmptyTable\":   \"No se encontraron resultados\",
				\"sInfo\":         \"Mostrando _START_ a _END_ de _TOTAL_ filas\",
				\"sInfoEmpty\":    \"No se encontraron resultados\",
				\"sInfoFiltered\": \"(Filtrado de _MAX_ total de filas)\",
				\"sLengthMenu\":   \"Ver _MENU_ filas\",
				\"sSearch\":       \"Buscar:\",
				\"sZeroRecords\":  \"No se encontraron resultados\"
			},
			\"aoColumnDefs\": [
			    { \"bSortable\": false, \"aTargets\": [ 0,-1 ] }

, { "iDataSort": 1, "aTargets": [ 0 ] },

				{ \"sWidth\": \"20px\",   \"aTargets\": [ 0,-1 ]  },

				
            ]
        });


        table.on(\"keyup\", \"input[type=search]\", function () {
		    /* Filter on the column (the index) of this element */
		    oTable.fnFilter(this.value, $(\"thead input\").index(this));
	    });

        $('#sample_1_tools > li > a.tool-action').on('click', function() {
            var action = $(this).attr('data-action');
            //console.log(action);
            oTable.DataTable().button(action).trigger();
        });

`

I solved this little problem as follows replaces: ajax: ‘resource’,

by: ajax: { url: ‘resource’, headers: {‘X-CSRF-TOKEN’: token}, dataType: ‘JSON’, beforeSend: function (xhr) { xhr.setRequestHeader(‘Authorization’); } },

Some tips

  1. Ajax maybe call doing POST or GET so you need configure your route in ANY
Route::any('campos/data','CamposController@data')->name('campos.data');
  1. Dont forget add the token before the ajax calling (in my case is Laravel)
$.ajaxSetup({
    headers: {
        'X-CSRF-TOKEN': '{{csrf_token()}}' 
    }
});
  1. I have that issue in using php artisan on Windows. If you use on Linux or Apache you wont have errors.
  2. Error 401 is becouse the session was expired. You need a login again. You could handler the error and take some action
"ajax": "/resource",
    "error": function(reason) {
        console.log("error encountered ! ");
        // process reason here to know the type of the error
        // and then take appropriate action
    }

Hello, I’ve encountered this issue and tried to debug for hours but to no avail. It seems that one workaround is to set your ajax as a POST request instead of GET This worked for me and i tested by refreshing my page 50 times without getting kicked out to the login page

The workaround is based on this issue https://github.com/yajra/laravel-datatables/issues/19

I’m using apache on a Fedora machine, using locally via the hosts file and i’m having this exact same issue. Any ideas?