我有一个使用 Ajax 数据源的 jQuery 数据表。该表定义如下:
$("#tblNotes").DataTable({
"ajax" : {
"url": "/projects/ajaxGetProjectNotes/",
"type" : "post",
"dataType" : "json",
"data" : {"idProject" : "b92792db-9ea6-49bf-b4dc-1cdf3f441148"}
},
"columns" :
[
{"data" : "dComment", "width" : "130px", "orderable" : true},
{"data" : "cComment", "width" : "270px", "orderable" : false}
]
});
php 脚本的 ajax 响应是:
[{"dComment":"","cComment":""}]
我收到错误:
TypeError: aData is undefined
for ( i=0 ; i<aData.length ; i++ ) {
-------------^
错误来自 datatables.js(第 15748 行,第 18 栏)。
这是 ajax 源的源(以及检索数据的随附模型函数)。请注意,这是基于 codeigniter 的代码(但我认为这不会产生太大的差异):
function ajaxGetProjectNotes(){
$id = $_POST["idProject"];
$notes = $this->projects_model->getNotes($id);
if(!isset($notes[0]["cComment"])){
$notes[0]["dComment"]="";
$notes[0]["cComment"]="";
}
echo json_encode($notes);
}
型号代码:
function getNotes($idProject){
$this->db->select("*")
->from("projectnotes")
->where("idProject", $idProject);
$query = $this->db->get();
$aResult = $query->result_array();
return $aResult;
}
页面加载时发生错误。
如有任何帮助,我们将不胜感激...
官方example解释说,平面数据源应该在选项中使用
dataSrc: ''
,你的选项将如下所示:
$("#tblNotes").DataTable({
"ajax" : {
"url": "/projects/ajaxGetProjectNotes/",
"type" : "post",
"dataType" : "json",
"data" : {"idProject" : "b92792db-9ea6-49bf-b4dc-1cdf3f441148"},
"dataSrc": '', // Add this one!
},
"columns" :
[
{"data" : "dComment", "width" : "130px", "orderable" : true},
{"data" : "cComment", "width" : "270px", "orderable" : false}
]
});