【问题标题】:Error on send an Ajax request to Controller, it isn't going as a json type向 Controller 发送 Ajax 请求时出错,它不是 json 类型
【发布时间】:2019-09-23 13:22:21
【问题描述】:

我正在运行 Mojave / Laravel 5.8 / PHP 7.2 / Jquery.dataTables 10.1.19。

第一轮:
如果我不放:if (!$.fn.DataTable.isDataTable('.table')) {...
我收到此错误:DataTables warning: table id=DataTables_Table_0 - Cannot reinitialise DataTable.
我不知道为什么,但我把这个 IF 和它的工作。我还有其他项目可以在没有它的情况下工作。

第二轮:
在我的索引中,我调用 DataTable() 调用 Ajax 请求来填充表格,因此 Ajax URL 是(从资源自动创建的)到控制器的路由。
在我的控制器中,我有一个函数 INDEX,它将接收请求,但需要检查它是来自 Ajax 请求(请求必须是 JSON)还是来自视图请求。
我不知道为什么它总是非 Json 类型。
所以,我收到了这个错误:DataTables warning: table id=DataTables_Table_0 - Invalid JSON response.

index.blade.php

            <div class="table-responsive">

               <table class="table table-bordered table-striped table-hover">
                    <thead>
                        <tr>
                            <th>Nome</th>
                            <th>NIF</th>
                            <th>Email</th>
                        </tr>
                    </thead>
                </table>
              </div>
            .
            .
            .
            <script type="text/javascript">   
               if (!$.fn.DataTable.isDataTable('.table')) {     
                  $('.table').DataTable({
                     "ajax": "{{ route('usuarios.index') }}",
                     "responsive": true,
                     "processing": true,
                     "serverSide": true,
                     "columns": [
                            { "data": "name" },
                            { "data": "nif" },
                            { "data": "email" },
                     ]
                  });
               };
            </script>   

UsuariosController.php

public function index(Request $request){ 
   if (request()->wantsJson()) {

       NEVER HERE 

       return ...

    }

    return view('admin.acesso.usuarios.index');
 }

Routes.php

Route::group(['prefix' => 'acesso'], function () {
    Route::resource('usuarios', "Acesso\UsuariosController");
});

【问题讨论】:

  • 您能否详细说明您得到的假设是json 响应?
  • 问题不是 json 响应,而是最初的 Ajax 请求。因此,函数(在控制器中)如何没有收到 Request()->wantsJson,它没有进入准备 json 响应。我猜我初始化 DataTable 时 Ajax 请求有问题或遗漏了。

标签: php ajax laravel datatables


【解决方案1】:

错误:DataTables 警告:table id=DataTables_Table_0 - 无法重新初始化 DataTable。

您无法重新初始化数据表。你必须摧毁它。

if ($.fn.DataTable.isDataTable("#your_table")) {
  $('#mytable').DataTable().clear().destroy();
}

$("#your_table").dataTable({
     ...              
});

DataTables 警告:表 id=DataTables_Table_0 - JSON 响应无效。

表示响应,不包含所有指定的数据。在您的代码中,您有以下内容

          "columns": [
                    { "data": "name" },
                    { "data": "nif" },
                    { "data": "email" },
             ]

您的回复,需要定义姓名、nig 和电子邮件。

例子

...
$dataRes = [
  [
      "name" => "test name",
      "nif" => "test nif",
      "email" => "test email",
  ],
    [
        "name" => "test name",
        "nif" => "test nif",
        "email" => "test email",
    ],
];

$response = [
    'data' => $dataRes,
    'draw' => $_POST['draw'], // this is from your param
    'recordsFiltered' => sizeof($dataRes), // this is if your using search
    'recordsTotal' => sizeof($dataRes),
];
return $response

【讨论】:

  • 问题就在这里:如果 (request()->wantsJson())... 请求总是 JSON:NULL 然后不会进入那里,在那里准备响应。跨度>
猜你喜欢
  • 1970-01-01
  • 2023-03-18
  • 1970-01-01
  • 1970-01-01
  • 2017-11-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-07-01
相关资源
最近更新 更多