[英]Error on send an Ajax request to Controller, it isn't going as a json type
I am running Mojave / Laravel 5.8 / PHP 7.2 / Jquery.dataTables 10.1.19. 我正在运行Mojave / Laravel 5.8 / PHP 7.2 / Jquery.dataTables 10.1.19。
First round: 第一回合:
If I don't put: if (!$.fn.DataTable.isDataTable('.table')) {...
如果我不放: if (!$.fn.DataTable.isDataTable('.table')) {...
I got this error: DataTables warning: table id=DataTables_Table_0 - Cannot reinitialise DataTable.
我收到此错误: DataTables warning: table id=DataTables_Table_0 - Cannot reinitialise DataTable.
I don't know exactly why, but I put this IF and it worked. 我不确切知道为什么,但我把这个IF和它起作用了。 I have other project where it works without that. 我有其他项目,没有它的工作。
Second round: 第二轮:
In my index I call DataTable()
that call an Ajax request to fill the table, so the Ajax URL is a route (from resource, created automatically) to a controller. 在我的索引中,我调用DataTable()
调用Ajax请求来填充表,因此Ajax URL是一个路径(从资源,自动创建)到控制器。
In my controller I have a function INDEX who will receive the request, but it needs to be checked if came from an Ajax request (the request must be a JSON) or from a view request. 在我的控制器中,我有一个函数INDEX,它将接收请求,但需要检查是否来自Ajax请求(请求必须是JSON)或来自视图请求。
I don't know why it always got a not Json type. 我不知道为什么它总是得到一个不是Json类型。
So, I got this error: DataTables warning: table id=DataTables_Table_0 - Invalid JSON response.
所以,我收到了这个错误: DataTables warning: table id=DataTables_Table_0 - Invalid JSON response.
index.blade.php 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 UsuariosController.php
public function index(Request $request){
if (request()->wantsJson()) {
NEVER HERE
return ...
}
return view('admin.acesso.usuarios.index');
}
Routes.php routes.php文件
Route::group(['prefix' => 'acesso'], function () {
Route::resource('usuarios', "Acesso\UsuariosController");
});
error: DataTables warning: table id=DataTables_Table_0 - Cannot reinitialise DataTable. 错误:DataTables警告:table id = DataTables_Table_0 - 无法重新初始化DataTable。
You cannot reinitialize a data table. 您无法重新初始化数据表。 you have to destroy it. 你必须摧毁它。
if ($.fn.DataTable.isDataTable("#your_table")) {
$('#mytable').DataTable().clear().destroy();
}
$("#your_table").dataTable({
...
});
DataTables warning: table id=DataTables_Table_0 - Invalid JSON response. DataTables警告:table id = DataTables_Table_0 - 无效的JSON响应。
means the response, does not contain all the specified data. 表示响应,不包含所有指定的数据。 in your code, you have the following 在您的代码中,您有以下内容
"columns": [
{ "data": "name" },
{ "data": "nif" },
{ "data": "email" },
]
your response, needs to have name, nig and email defined. 您的回复,需要定义名称,nig和电子邮件。
example 例
...
$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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.