laravel json 响应没有打印在数据表中
laravel json response doesnt get printed in datatable
在我的 web.php 我有一个模型,我转向 json:
Route::get('operatore/ajax',function(PdfDettagli $PdfDettagli){
return $PdfDettagli::all()->toJson();
});
在我看来我有 table:
<table id="table" class="display" style="width:100%">
<thead>
<tr>
<th>nome</th>
<th>posizione</th>
</tr>
</thead>
</table>
以及以下 jquery:
script type="text/javascript">
$(document).ready(function(){
$('#table').DataTable({
"ajax": "/operatore/ajax",
"columns": [
{"data":"nome" },
{"data":"posizione"}
]
});
});
</script>
它应该将模型读取为 json 并自动上传填充 table 但它不会这样做
table是这样的:https://datatables.net/
在手册中,我阅读了有关 ajax 的部分:https://datatables.net/manual/ajax
我想我已经像示例中那样做了,但它不起作用
如果我进入 operatore/ajax,返回的响应格式如下:
[{"id":1,"posizione":"operatori\/Giuseppe-Pentangelo\/pdf\/carta-di-identita\/K3hXUVBjUZqBMLgWG2SbMCMdLVK5mcTJTcusSImB.png","nome":"img_avatar.png","id_veicolo":null,"id_operatore":1,"id_categoria_pdf":1,"created_at":"2022-02-15T12:02:53.000000Z","updated_at":"2022-02-15T12:02:53.000000Z"},
我也试过 model->asArray,它给出的响应不同,但都不起作用
编辑:浏览器控制台给出此错误:
Uncaught TypeError: f is undefined
jQuery 17
<anonymous> http://localhost:8000/pdf:12
jQuery 13
jquery.dataTables.min.js:49:73
jQuery 17
ha
i
success
c
fireWith
l
o
(Asinc.: EventHandlerNonNull)
send
ajax
sa
ha
e
n
each
each
n
DataTable
<anonima> http://localhost:8000/pdf:12
jQuery 13
e
t
(Asinc.: setTimeout handler)
l
c
fireWith
fire
c
fireWith
ready
B
(Asinc.: EventListener.handleEvent)
<anonima>
<anonima>
<anonima>
默认情况下,DataTable 需要这种格式的响应:
{
"data" : [...]
}
您的端点直接 returns 数据数组。
要解决您的问题,只需修改您的 DataTable 配置:
"ajax": "/operatore/ajax"
至:
"ajax": {
"url": "/operatore/ajax",
"dataSrc": ""
}
来源:https://datatables.net/examples/ajax/custom_data_property.html
在我的 web.php 我有一个模型,我转向 json:
Route::get('operatore/ajax',function(PdfDettagli $PdfDettagli){
return $PdfDettagli::all()->toJson();
});
在我看来我有 table:
<table id="table" class="display" style="width:100%">
<thead>
<tr>
<th>nome</th>
<th>posizione</th>
</tr>
</thead>
</table>
以及以下 jquery:
script type="text/javascript">
$(document).ready(function(){
$('#table').DataTable({
"ajax": "/operatore/ajax",
"columns": [
{"data":"nome" },
{"data":"posizione"}
]
});
});
</script>
它应该将模型读取为 json 并自动上传填充 table 但它不会这样做
table是这样的:https://datatables.net/ 在手册中,我阅读了有关 ajax 的部分:https://datatables.net/manual/ajax 我想我已经像示例中那样做了,但它不起作用
如果我进入 operatore/ajax,返回的响应格式如下:
[{"id":1,"posizione":"operatori\/Giuseppe-Pentangelo\/pdf\/carta-di-identita\/K3hXUVBjUZqBMLgWG2SbMCMdLVK5mcTJTcusSImB.png","nome":"img_avatar.png","id_veicolo":null,"id_operatore":1,"id_categoria_pdf":1,"created_at":"2022-02-15T12:02:53.000000Z","updated_at":"2022-02-15T12:02:53.000000Z"},
我也试过 model->asArray,它给出的响应不同,但都不起作用
编辑:浏览器控制台给出此错误:
Uncaught TypeError: f is undefined
jQuery 17
<anonymous> http://localhost:8000/pdf:12
jQuery 13
jquery.dataTables.min.js:49:73
jQuery 17
ha
i
success
c
fireWith
l
o
(Asinc.: EventHandlerNonNull)
send
ajax
sa
ha
e
n
each
each
n
DataTable
<anonima> http://localhost:8000/pdf:12
jQuery 13
e
t
(Asinc.: setTimeout handler)
l
c
fireWith
fire
c
fireWith
ready
B
(Asinc.: EventListener.handleEvent)
<anonima>
<anonima>
<anonima>
默认情况下,DataTable 需要这种格式的响应:
{
"data" : [...]
}
您的端点直接 returns 数据数组。
要解决您的问题,只需修改您的 DataTable 配置:
"ajax": "/operatore/ajax"
至:
"ajax": {
"url": "/operatore/ajax",
"dataSrc": ""
}
来源:https://datatables.net/examples/ajax/custom_data_property.html