[英]No data returned to jquery datatables from ajax call
這是我第一次嘗試用datatables
。 我想調用ajax
函數以從數據庫( MariaDB
)返回數據,並在php
文件中填充現有table
的rows
和cells
。
關於數據字段的代碼的簡化版本是這樣的:
$('#tbl_resultados').dataTable( {
"bProcessing": true,
"sAjaxSource": "/apls/indicadores/indic_acess.php",
"sAjaxDataProp": "data",
"fnServerData": function ( sSource, aoData, fnCallback ) {
request = $.ajax({
"dataType": 'json',
"type": "GET",
"url": sSource,
"data":{
"fn": "lines",
"indic_per": indic_per,
},
"success": fnCallback
});
},
"aoColumns": [
{ "mDataProp": "id"},
{ "mDataProp": "per"},
],
} );
我嘗試使用"bServerSide": true,
和不使用"bServerSide": true,
但是無法獲取要在table
顯示的數據。 Firebug
返回TypeError: aData is undefined
。
按照示例中的說明,我在php
中構建了一個函數,該函數返回類似於json
結構。
function getLines()
{
echo "{ \"aData\":[
[
\"25983\",
\"2010\"
],
[
\"90420\",
\"2011\"
]
]
}";
}
就像我說的,我已經沒有任何經驗datatables
不知道是否使用aData
或aaData
,還是什么嘗試更多。
這是ajax源的另一個示例:
http://datatables.net/examples/data_sources/ajax.html
它說 :
DataTables expects an object with an array called "aaData" with the data source.
這是他們的一些ajax返回:
{ "aaData": [
["Trident","Internet Explorer 4.0","Win 95+","4","X"],
["Trident","Internet Explorer 5.0","Win 95+","5","C"],
["Trident","Internet Explorer 5.5","Win 95+","5.5","A"],
["Trident","Internet Explorer 6","Win 98+","6","A"],
["Trident","Internet Explorer 7","Win XP SP2+","7","A"],
.
.
.
["Misc","Links","Text only","-","X"],
["Misc","Lynx","Text only","-","X"],
["Misc","IE Mobile","Windows Mobile 6","-","C"],
["Misc","PSP browser","PSP","-","C"],
["Other browsers","All others","-","-","U"]
] }
在我這邊,我總是使用aaData
來初始化表數據。
除此之外,您可以返回更多選項(例如sEcho
, iTotalRecords
, iTotalDisplayRecords
,...)以完成渲染。
Datatables.net示例不適用於MariaDB服務器(與使用root至舊密碼加密格式的用戶登錄相同)。 我不知道為什么不這樣做,但是它不響應任何數據(ajax)。 在Windows MariaDB服務器上測試。
解決方案:如果您不想卸載MariaDB服務器,則在其他端口上安裝MySQL服務器服務/守護程序,然后遷移(導出/導入)數據。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.