在我的 web.php 中,我有一個使用 json 的模型:
Route::get('operatore/ajax',function(PdfDettagli $PdfDettagli){
return $PdfDettagli::all()->toJson();
});
在我看來,我有表:
<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 并自動上傳填充表格,但它不會這樣做
表格是這樣的: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>
uj5u.com熱心網友回復:
默認情況下,DataTable 需要以下格式的回應:
{
"data" : [...]
}
相反,您的端點直接回傳資料陣列。
要解決您的問題,只需從以下位置修改您的 DataTable 配置:
"ajax": "/operatore/ajax"
到:
"ajax": {
"url": "/operatore/ajax",
"dataSrc": ""
}
來源:https ://datatables.net/examples/ajax/custom_data_property.html
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/426315.html
標籤:javascript php jQuery 阿贾克斯 拉拉维尔
