数据表:遗漏的类型错误:无法读取的未定义的属性“长度”(Datatables: Uncaught T

2019-07-17 15:38发布

我试图使用AJAX源与数据表,我已经碰到在做一些错误。 此前阿贾克斯没有被与数据表中,并且他们工作的罚款,但在尝试使用Ajax和JSON我有一些错误。

我recieving错误如下:

遗漏的类型错误:无法读取的未定义的属性“长度”

编辑:在使用正下方这段文字修改后的代码,这个错误不再出现,但数据表仍破(无觅处,分页,排序等)。 它可以帮助有一个活生生的例子,那么试试这个网站: 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开发和实施数据表已经相当的学习曲线!

Answer 1:

你的JSON输出是错误的,原因如下:

  1. iTotalRecordsiTotalDisplayRecords字段缺失。 这就是为什么分页(和所有其它功能性)被破坏(请注意在页脚部分“显示1为NaN条目的NaN(来自NaN的总条目过滤)”的消息)。 请参阅此页有关服务器端处理的进一步细节。
  2. 还有就是JSON响应之后,一些HTML代码。

这里是(摘自额外的HTML代码test.php的 ):

<!-- Hosting24 Analytics Code -->
<script src="http://stats.hosting24.com/count.php"></script>
<!-- End Of Analytics Code -->

在我看来,在test.php脚本应该是这样的:

<?php 

$link = mysqli_init();

// Adjust hostname, username, password and database name before use!
$db = mysqli_real_connect($link, "hostname", "username", "password", "database") or die(mysqli_connect_error());

$SQL = 'SELECT `id`,`Minecraft_Username`,`Block_Name`,`Quantity`,`Cost`,`Trade_Status` FROM mctrade_trades';
$result = mysqli_query($link, $SQL) or die(mysqli_error($link));

$aaData = array();
while ($row = mysqli_fetch_assoc($result)) {
    $aaData[] = $row;
}

$response = array(
  'aaData' => $aaData,
  'iTotalRecords' => count($aaData),
  'iTotalDisplayRecords' => count($aaData)
);
if (isset($_REQUEST['sEcho'])) {
  $response['sEcho'] = $_REQUEST['sEcho'];
}

header('Content-type: application/json'); 
echo json_encode($response);

?>

也知道, mysql_*功能被弃用,因此,你应该使用PDO或MySQLi的替代; 看看这个答案对于进一步的细节。



文章来源: Datatables: Uncaught TypeError: Cannot read property 'length' of undefined