javascript - Laravel datatable invalid JSON response -
everything work fine on code below fetch user logs:
js:
<script> $(document).ready(function () { var table =$('#systemlogs').datatable({ responsive: true, processing: true, serverside: true, "language": { "url": "/datatables/media/plug-in/persian.json" }, ajax: '{!! url('/admin/systemlogs/data/systemlogsdatatable') !!}', columns: [ { data: 'name', name: 'name' }, { data: 'message_text', name: 'message_text' }, { data: 'remote_addr', name: 'remote_addr' }, { data: 'log_created_at', name: 'management_logs.created_at' }, { data: 'log_updated_at', name: 'management_logs.updated_at' }, ] }); }); </script>
routes:
route::get('/systemlogs/data/systemlogsdatatable','systemlogscontroller@systemlogsdatatable'); public function systemlogsdatatable() { $logs= managementlog::select('management_logs.message_text','management_logs.remote_addr','management_logs.created_at log_created_at','management_logs.updated_at log_updated_at','managements.name')->leftjoin('managements','management_logs.management_id','=','managements.id'); return datatables::of($logs)->make(true); }
it works fine me when want fetch 2 fields database give me
datatables warning:table id=systemlogs - invalid json response.
but times work ok.
<script> $(document).ready(function () { var table =$('#systemlogs').datatable({ responsive: true, processing: true, serverside: true, "language": { "url": "/datatables/media/plug-in/persian.json" }, ajax: '{!! url('/admin/systemlogs/data/systemlogsdatatable') !!}', columns: [ { data: 'name', name: 'name' }, { data: 'message_text', name: 'message_text' }, ] }); }); </script>
i have understood problem middleware:
public function handle($request, closure $next,$guard = 'admin') { if(!auth::guard($guard)->check()){ return redirect('/administrator/logout'); }else{ return $next($request); } }
when changed to:
public function handle($request, closure $next,$guard = 'admin') { return $next($request); }
it works fine means problem ajax. have do?
it's because if you're not logged application response gonna redirect , invalid ajax response, can try handle redirect response:
$(document).ready(function () { var table =$('#systemlogs').datatable({ responsive: true, processing: true, serverside: true, "language": { "url": "/datatables/media/plug-in/persian.json" }, ajax: function(data, callback, settings) { // make regular ajax request $.get('{!! url('/admin/systemlogs/data/systemlogsdatatable') !!}', function(res) { if(res.redirect) window.location.href = res.redirect; callback({ data: json.parse(res) }); }); }, columns: [ { data: 'name', name: 'name' }, { data: 'message_text', name: 'message_text' }, { data: 'remote_addr', name: 'remote_addr' }, { data: 'log_created_at', name: 'management_logs.created_at' }, { data: 'log_updated_at', name: 'management_logs.updated_at' }, ] }); });
Comments
Post a Comment