5 months ago

Failed ajax call to Datatables. returns 404 not found

Posted 5 months ago by Zaheer

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">
                <th width="50px">Action</th>
                <th>Ref. No.</th>
                <th>Inv. No.</th>
                <th>Inv. Date</th>
                <th>Net Total</th>



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)
        if ($branch_id==0){
            return redirect()->route('login');

       if ($request->ajax()) {
            $data = DB::table('purchases')
                        WHEN purchases.posted = "1" THEN "Posted" 
                        ELSE "Unposted" 
                        END) AS status'),

            return Datatables::of($data)
                ->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;
        return view('purchases',compact('purchases'));

Please sign in or create an account to participate in this conversation.