0
votes

I am using DataTables 1.10.16 and using Ajax I am trying to build the table.

The table works fine in my backend but in frontend I am getting 302 redirect.

But if I call the Ajax URL in the browser it returns the data correctly.

I am not getting where I am going wrong

This is how initialize DataTable

var datatable = $('#reports-info-table');
        datatable.DataTable({
            "processing": true,
            "serverSide": true,
            "ajax": BASE_URL + '/user/reported-business-info',
            "columns": [{
                data: 'slno',
                name: 'slno'
            },
                {
                    data: 'report',
                    name: 'report'
                },
                {
                    data: 'business_name',
                    name: 'business_name'
                },
                {
                    data: 'show',
                    name: 'show',
                    orderable: false,
                    searchable: false
                }
            ]
        });

This is how return the DataTable

return Datatables::of($reports)
    ->editColumn('report', function ($reports) {
            return $reports->vchr_report_message;
    })
    ->editColumn('business_name', function ($reports) {
        return $reports->business->vchr_business_name;
    })
    ->addColumn('show', function ($reports) {
        if ($reports->int_status == 1) {
            return '
                    <div class="btn-group btn-collection btn-icon-group">
                        <a class="btn btn-info" data-toggle="tooltip" title="View"> <i class="ua-icon-control-export btn-icon"></i></a>
                    </div>';
        } //If Status is not in active state
        else {
            return '
                    <div class="btn-group btn-collection btn-icon-group">
                        <a class="btn btn-info" data-toggle="tooltip" title="View"> <i class="ua-icon-control-export btn-icon"></i></a>
                    </div>';
        }
    })
        ->rawColumns(['show'])->toJson(true);
1

1 Answers

0
votes

I found out that my middleware was stopping the Ajax call and hence getting redirected.

Original Code

if (!$request->ajax() && Auth::check() && (Auth::user()->int_role_id == Roles::ROLE_USER)) {
        return $next($request);
    }else if (!$request->ajax() && !Auth::check() ){
        $url = Request()->path();
        Session::put('loginRedirect', $url);
        return redirect('/login');
    }

Changed TO

if (Auth::check() && (Auth::user()->int_role_id == Roles::ROLE_USER)) {
        return $next($request);
    }else if (!$request->ajax() && !Auth::check() ){
        $url = Request()->path();
        Session::put('loginRedirect', $url);
        return redirect('/login');
    }

Now it Works.