我正在尝试将 ajax 源与 Datatables 一起使用,但在执行过程中遇到了一些错误。以前 Ajax 没有与 Datatables 一起使用,它们工作正常,但是在尝试使用 Ajax 和 JSON 时出现了一些错误。
我收到的错误如下:
未捕获的类型错误:无法读取未定义的属性“长度”
编辑:直接在此文本下方使用修改后的代码后,此错误不再存在,但 DataTables 仍然损坏(没有搜索、分页、排序等......)。有一个活生生的例子可能会有所帮助,所以试试这个网站:fogest.com/test
在此处加载页面时创建表是代码:
$(document).ready(function() {
$('#trades').dataTable( {
"sDom": "<'row'<'span6'l><'span6'f>r>t<'row'<'span6'i><'span6'p>>",
"sPaginationType": "bootstrap",
"bProcessing": true,
"bServerSide": true,
"aoColumns": [
{ "mData": "id" },
{ "mData": "Minecraft_Username" },
{ "mData": "Block_Name" },
{ "mData": "Quantity" },
{ "mData": "Cost" },
{ "mData": "Trade_Status" },
],
"sAjaxSource": "test.php"
} );
} );
sAjaxSource test.php 包含以下内容:
<?php
$tableName = "mctrade_trades";
$result = mysql_query("SELECT `id`, `Minecraft_Username`, `Block_Name`, `Quantity`, `Cost`, `Trade_Status` FROM $tableName");
$data = array();
while ( $row = mysql_fetch_assoc($result) )
{
$data[] = $row;
}
header("Content-type: application/json");
echo json_encode( $data );
?>
test.php 的输出:
[{"id":"1","Minecraft_Username":"fog","Block_Name":"DIAMOND_PICKAXE","Quantity":"1","Cost":"100","Trade_Status":"1"},{"id":"2","Minecraft_Username":"fog","Block_Name":"DIAMOND_PICKAXE","Quantity":"1","Cost":"1002","Trade_Status":"1"},{"id":"3","Minecraft_Username":"fog","Block_Name":"DIAMOND_PICKAXE","Quantity":"1","Cost":"1035","Trade_Status":"1"},{"id":"4","Minecraft_Username":"fog","Block_Name":"DIAMOND_PICKAXE","Quantity":"1","Cost":"1035","Trade_Status":"1"},{"id":"5","Minecraft_Username":"fog","Block_Name":"DIAMOND_PICKAXE","Quantity":"1","Cost":"100","Trade_Status":"2"},{"id":"6","Minecraft_Username":"fog","Block_Name":"DIAMOND_PICKAXE","Quantity":"1","Cost":"100","Trade_Status":"2"},{"id":"7","Minecraft_Username":"fog","Block_Name":"DIAMOND_PICKAXE","Quantity":"1","Cost":"10000","Trade_Status":"2"}]
表格已正确生成,但由于此错误,在表格上方显示“正在处理,您无法使用数据表的任何功能,例如搜索。
这是使用上述 JSON 的表格的图像:
我假设错误在我的 JSON 输出中,但我不完全知道它有什么问题,也不知道我应该做什么来修复它。我是 Web 开发的新手,实现 Datatables 的学习曲线相当不错!