【发布时间】:2020-03-06 10:42:07
【问题描述】:
我在 Codeigniter 中使用 Ignited Datatables 来显示来自数据库的数据。它工作正常,数据显示正常。但是当我在数据表中搜索特定数据时(使用数据表搜索框),它会显示如下错误:
DataTables 警告:table id=table - Ajax 错误。有关此错误的更多信息,请参阅http://datatables.net/tn/7
控制器:
public function json(){
header('Content-Type: application/json');
echo $this->m_bahan->json();
}
型号:
function json() {
$this->datatables->select('*');
$this->datatables->from('t_bahan');
return $this->datatables->generate();
}
查看:
<table id="table" class="table table-striped table-bordered" width="100%">
<thead>
<tr>
<th width="5%">No</th>
<th>Nama Bahan Dasar</th>
<th width="175px"></th>
</tr>
</thead>
</table>
Javascript:
table = $('#table').DataTable({
processing: true, //Feature control the processing indicator.
serverSide: true, //Feature control DataTables' server-side processing mode.
order: [[1, 'desc']], //Initial order.
// Load data for the table's content from an Ajax source
ajax: {
url: "bahan/json",
type: "POST"
},
//Set column definition initialization properties.
columns: [
{
data: 'id_bahan',
orderable: false,
render: function (data, type, row, meta) {
return meta.row + meta.settings._iDisplayStart + 1;
}
},
{data: 'nama_bahan'},
{
className: 'center',
orderable: false,
defaultContent: '<button class="btn btn-warning fa fa-edit" data-toggle="modal" data-target="#editModal"> Ubah</button> <emsp/> <button name="delete" class="btn btn-danger fa fa-trash"> Hapus</button>'
}
]
});
但是,当我直接从浏览器访问 bahan/json url 时,它会打印 json 结果。
这里有什么问题?
【问题讨论】:
-
500 错误很可能是查询模型中的数据的问题。在您的模型中,在返回之前尝试:
print_r($this->datatables->generate());die;它输出什么?检查浏览器网络控制台,您可能会看到数据库错误。 -
@Vickel 您可以在上图中看到。它不是返回 json,而是一个网页。
-
不,它会告诉你到底出了什么问题:
error undefined index: id_bahan in libraries line 331。现在你需要找出索引未定义的原因 -
是的,我知道。你知道这里有什么问题吗?我为此使用了 Ignite Datatables 库。
-
检查您的列定义:
data: 'id_bahan',也许您在t_bahan表结构中使用了另一个不是id_bahan的列名
标签: json ajax codeigniter datatable datatables