Ad

DataTables Json Is Not Processed (in Laravel)

Datatables does nothing with the json it requested

I am using Yajra's Datatables to populate my table and I did everything by the book. The controller is set up correctly, the routes are good (if I enter the route designated to the controller function which creates the Database, I can see the desired json in the desired format:

{"draw":0,
 "recordsTotal":44,
 "recordsFiltered":44,
 "data":[{
          "agency":"agency",
          "number":"20966512",
          "name":"Name John",
          "value":"28.22",
        }]
}

This is the .js code that I use to create my datatable:

$(document).ready(function() {
  $('#tbl').DataTable({
    processing: true,
    serverSide: true,
    ajax: '{!! route("datatable") !!}',
    columns: [
        {data: 'agency', name: 'agency'},
        {data: 'number', name: 'number'},
        {data: 'name', name: 'name'},
        {data: 'value', name: 'value'},
    ]
  });
});

I am getting an error stating:

DataTables warning: table id=tbl - Ajax error. For more information about this error, please see http://datatables.net/tn/7

When I look in the console I get the following error:

{
"message": "",
"exception": "Symfony\\Component\\HttpKernel\\Exception\\NotFoundHttpException",
"file": "/home/vagrant/Projects/Uniqa-ACB/vendor/laravel/framework/src/Illuminate/Routing/RouteCollection.php",
"line": 179,
"trace": etc...
}

Laravel 5.8, PHP 7.3, Yajra DT: 6, datatables.net: 1.10

Thank you in advance, if you need any more code I wil gladly input it.

Ad

Answer

The route that was trying to access contained the full json with some weird encoding on it, I changed the route to "/datatables" directly and as recommended bellow, I also specified the post type to GET, and now the tables are being populated. thank you.

Ad
source: stackoverflow.com
Ad