DataTables json is not processed (in Laravel)

纵然是瞬间 提交于 2019-12-14 02:31:47

问题


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.


回答1:


replace ajax with this

ajax: {
    "url": "{!! route("datatable") !!}",
    "type": "POST",
  }



回答2:


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.



来源:https://stackoverflow.com/questions/55647900/datatables-json-is-not-processed-in-laravel

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!