【问题标题】:JSON showing instead of Datatable following Ajax call to Laravel Controller在 Ajax 调用 Laravel 控制器后显示 JSON 而不是 Datatable
【发布时间】:2019-04-05 14:07:56
【问题描述】:

我对 ajax 和 json 还很陌生,非常感谢一些帮助。 我正在对 Laravel 控制器进行 Ajax 调用,以从名为“subjects”的数据库表中返回一些字段,并将它们显示在 Laravel 视图的 DataTable 中。问题是当我打开视图时,看到的是 JSON 而不是 Datatable。

以下是视图主题/索引中返回的内容:

{"draw":0,"recordsTotal":8,"recordsFiltered":8,"data":[{"id":"1","name":"Biology"},{"id":"3","name":"English Language"},{"id":"4","name":"Physics"},{"id":"5","name":"Chemistry"},{"id":"6","name":"Mathematics"},{"id":"7","name":"Mathematics"},{"id":"8","name":"English Language"},{"id":"9","name":"French"}],"queries":[{"query":"select count(*) as aggregate from (select '1' as `row_count` from `subjects`) count_row_table","bindings":[],"time":4.65},{"query":"select `id`, `name` from `subjects`","bindings":[],"time":0.41}],"input":[]}

这是视图 /subjects/index 中的 HTML

<table id="subjects_table" class="table table-bordered" style="width:100%">
    <thead>
            <tr>
                <th>Id</th>
                <th>Subject</th>
            </tr>
                </thead>
                <tbody>

                </tbody>
        </table>

这是 Laravel 控制器中的代码:

class SubjectsController extends Controller
{
    public function index()
    {
        $subjects = Subject::select('id', 'name');
        return Datatables::of($subjects)->make(true);
   }
}

下面是进行 Ajax 调用的代码:

$('#subjects_table').DataTable({
      "processing": true,
      "serverSide": true,
      "ajax": "{{route('subjects.index')}}",
      "columns":[
          {"data": "id"},
          {"data": "name"}
      ]
});

这是 web.php 中的路由定义:

Route::get('subjects/', 'SubjectsController@index')->name('subjects.index');

如果您能提供任何帮助,我们将不胜感激

【问题讨论】:

    标签: ajax laravel datatables


    【解决方案1】:

    你应该试试这个:

    路线

    Route::get('subjects', 'SubjectsController@index')->name('subjects.index');
    Route::get('getsubjects', 'SubjectsController@getSubjects')->name('subjects.get');
    

    SubjectsController

    class SubjectsController extends Controller
    {
        public function index()
        {
            return view('subjects.index');
            $subjects = Subject::select('id', 'name');
            return Datatables::of($subjects)->make(true);
       }
    
       public function getSubjects()
        {
    
            return \DataTables::of(Subject::query())->make(true);
    
       }
    }
    

    查看

        <table id="subjects_table" class="table table-bordered" style="width:100%">
        <thead>
                <tr>
                    <th>Id</th>
                    <th>Subject</th>
                </tr>
                    </thead>
                    <tbody>
    
                    </tbody>
            </table>
    
    <script src="https://cdn.datatables.net/1.10.12/js/jquery.dataTables.min.js"></script>
        <script type="text/javascript">
        $(document).ready(function() {
            $('#subjects_table').DataTable({
                processing: true,
                serverSide: true,
                ajax: '{{ route('subjects.get') }}',
                columns: [
                    {data: 'id', name: 'id'},
                    {data: 'name', name: 'name'},
                    {data: 'email', name: 'email'},
                ]
            });
        });
        </script>
    

    【讨论】:

    • 非常感谢 Saurabh,它成功了。非常感谢这是否意味着无法使用单一路线完成?
    • @IsoB:不,您可以为数据表创建两条路由 1. 用于显示视图 2. 用于获取数据表数据
    • @IsoB:很高兴为您提供帮助,如果我的回答对您有用,请接受我的回答
    • 我如何“接受”你的回答?是否有一个按钮或我应该点击的东西?找不到
    • @IsoB 感谢您的回复,点击此链接应该是如何在 StackOverflow 中接受答案的指南(即stackoverflow.com/help/someone-answers
    猜你喜欢
    • 1970-01-01
    • 2012-10-27
    • 2020-05-12
    • 2018-04-19
    • 2020-12-19
    • 2018-03-02
    • 1970-01-01
    • 2017-10-01
    • 2012-09-09
    相关资源
    最近更新 更多