Could it be that login route name doesn't exist? I mean that would throw a 404 since you're redirecting to there!
Failed ajax call to Datatables. returns 404 not found
I am getting a DataTables warning: table id=DataTables_Table_0 - Ajax error. Correct data is returned most of the times but once every three or four calls this Warning appears with no data loaded in data tables. Can someone identify what is wrong with my code. I am a beginner in web development.
Upon debugging I found that no call to controller is sent when this warning appears. The console says 404 Not found. I think it is the route not found. But then why it is loading most of the times. I have also put a Info($request) in the controller, and Laravel log is empty when this error appears.
I am using Laravel 5.8. HTML code is :
<div class="box-body">
<table class="data-table display compact">
<thead>
<tr>
<th width="50px">Action</th>
<th>Date</th>
<th>Ref. No.</th>
<th>Supplier</th>
<th>Inv. No.</th>
<th>Inv. Date</th>
<th>Total</th>
<th>Freight</th>
<th>Discount</th>
<th>Cash</th>
<th>Net Total</th>
<th>Remarks</th>
<th>Status</th>
</tr>
</thead>
<tbody>
</tbody>
</table>
</div>
Ajax code is :
var table = $('.data-table').DataTable({
processing: true,
serverSide: true,
ajax: "{{ route('purchlist') }}",
columns: [
{data: 'action', name: 'action', orderable: false, searchable: false, width: "50px"},
{data: 'date', name: 'date'},
{data: 'refno', name: 'refno'},
{data: 'title', name: 'title'},
{data: 'sup_invno', name: 'sup_invno'},
{data: 'sup_invdate', name: 'sup_invdate'},
{data: 'total', name: 'total'},
{data: 'freight', name: 'freight'},
{data: 'discount', name: 'discount'},
{data: 'cash', name: 'cash'},
{data: 'nettotal', name: 'nettotal'},
{data: 'remarks', name: 'remarks'},
{data: 'status', name: 'status'},
]
});
And my Controller code is :
public function index(Request $request)
{
$branch_id=session()->get('lbranch','0');
if ($branch_id==0){
return redirect()->route('login');
}
if ($request->ajax()) {
$data = DB::table('purchases')
->where('purchases.branchid',$branch_id)
->where('accounts.branchid',$branch_id)
->leftjoin('accounts','purchases.account_code','=','accounts.code')
->select('purchases.*',
DB::raw('(CASE
WHEN purchases.posted = "1" THEN "Posted"
ELSE "Unposted"
END) AS status'),
'accounts.title')
->latest()->get();
return Datatables::of($data)
->addIndexColumn()
->addColumn('action', function($row){
$btn = '<a href="javascript:void(0)" data-toggle="tooltip" data-id="'.$row->id.'" data-original-title="Edit" class="edit btn btn-primary btn-sm editPurchase"><i class="fa fa-edit"></i></a>';
$btn = $btn.' <a href="javascript:void(0)" data-toggle="tooltip" data-id="'.$row->id.'" data-original-title="Delete" class="btn btn-danger btn-sm deletePurchase"><i class="fa fa-trash"></i></a>';
return $btn;
})
->rawColumns(['action'])
->make(true);
}
return view('purchases',compact('purchases'));
}
Please or to participate in this conversation.