jeudi 20 janvier 2022

My laravel Yajra datatable does not render. It says invalid json response. However I can not read the error since the response is empty

Hello I have the following controller method to return data to my datatable in Laravel,

Controller Method

public function get(Request $request) {
    return Datatables::of(AppUser::all())
    ->addColumn('status', function ($user) {
        if ($user->status == 1) {
            return '<span class="label label-success">Active</span>';
        } else {
            return '<span class="label label-danger">Inactive</span>';
        }
    })
    ->addColumn('actions', function ($user) {
        return view('backend.appuser.actionButton', compact('user'))->render();
    })
    ->make(true);
}

Then in the view I render the datatable, I have the following code.

        <table id="users-table" class="table table-condensed table-hover">
            <thead>
                <tr>
                    <th>Username</th>
                    <th>NIC</th>
                    <th>Mobile</th>
                    <th>Status</th>
                    <th>Actions</th>
                </tr>
            </thead>
        </table>

Inside my script tag I have the below code

$(function() {
    $('#users-table').DataTable({
        processing: true,
        serverSide: true,
        ajax: {
            url: '',
            type: 'get',
            data: {status: 1, trashed: false}
        },
        columns: [
            {data: 'email', name: 'email'},
            {data: 'nic', name: 'nic'},
            {data: 'mobile', name: 'mobile'},
            {data: 'status', name: 'status'},
            {data: 'actions', name: 'actions'}
        ],
        pageLength:25,
        lengthMenu:[[10,25,50,100,-1],[10,25,50,100,"All"]],
        order: [ [ 0, "desc" ] ],
        dom: "lBfrtip",
        buttons:
            [
                {extend: 'excel', footer: true, title: 'User Details'},
                {extend: 'pdf', footer: true, title: 'User Details', orientation: 'landscape', pageSize: 'LEGAL'},
                {extend: 'print', footer: true, title: 'User Details', orientation: 'landscape', pageSize: 'LEGAL'}
            ],

        searchDelay: 500
    });
});

The Error When I go to the index page that the datatable is loaded, it says, DataTables warning: table id=users-table - Invalid JSON response. For more information about this error, please see http://datatables.net/tn/1

What I tried

  1. I tried making a little syntax error in the above controller method to see if the application crashes. If the application crashes, it means that the request sent from the datatable must have hit my controller method. (App actually crashed, so the request from the datatable is coming to my controller method.)
  2. I went to the network tab in my developer tools and inspected the response for the request sent from the data-table. The response is empty. It just shows three empty lines. Since the response is empty, I can not figure out what the error is.

Below picture shows the response I got. Picture of the empty response

(I am using Laravel 5.4)



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire