我试图在Datatables中使用ajax数据源,但在操作过程中遇到了一些错误。以前并没有使用Ajax与Datatables,它们一直正常工作,但尝试使用Ajax和JSON后出现了一些错误。
我收到的错误如下:
未捕获的类型错误:无法读取未定义的'length'属性
编辑:在使用下面这段修订后的代码之后,不再出现此错误,但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"}]
表格已经正确生成,但由于这个错误,上面会显示“正在处理”文本,你不能使用任何Datatable的功能,如搜索。
以下是使用上述JSON生成的表格示例图像:
我猜测错误在我的JSON输出中,但我不知道具体是什么问题,也不知道应该怎么解决。我对Web开发还很陌生,实现Datatables一直是一个学习曲线!
sAjaxDataProp: ""
иЎҢпјҢдјҡеҸ‘з”ҹд»Җд№ҲпјҹжӮЁжҳҜеҗҰд»Қ然дјҡ收еҲ°зӣёеҗҢзҡ„й”ҷиҜҜпјҹ - Tanzeel Kazi