【问题标题】:JQuery Datatable Error "Cannot read property 'length' of undefined"JQuery 数据表错误“无法读取未定义的属性‘长度’”
【发布时间】:2015-08-22 08:43:05
【问题描述】:

我正在从我网站上名为 request.php 的另一个 URL 获取我的 DataTable 的数据。

request.php:

$con = mysql_connect("xxxx", "xxxx", "xxxx");
$con or die(mysql_error());
mysql_select_db("xxxx") or die(mysql_error());

$result = mysql_query("SELECT * FROM users_logins") 
or die(mysql_error()); 

$resultArray = array();
$tempArray = array();

while($row = mysql_fetch_assoc($result))
{
    // Add each row into our results array
    $tempArray = $row;
    array_push($resultArray, $tempArray);
}

echo json_encode($resultArray);

mysql_close($con);

这是我的 DataTable 的 HTML 和 JQuery。

table.html:

<head>
  <link rel="stylesheet" type="text/css" href="http://ajax.aspnetcdn.com/ajax/jquery.dataTables/1.9.4/css/jquery.dataTables.css">
</head>
<body>
 <table id="example" class="display" cellspacing="0" width="100%">
        <thead>
            <tr>
                <th>uEmail</th>
                <th>Time</th>
                <th>Location</th>
            </tr>
        </thead>


    </table>
  <script type="text/javascript" charset="utf8" src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.8.2.min.js"></script>
  <script type="text/javascript" charset="utf8" src="http://ajax.aspnetcdn.com/ajax/jquery.dataTables/1.9.4/jquery.dataTables.min.js"></script>
  <script>


    $(document).ready(function() {
        $('#example').dataTable( {
                "sAjaxSource": "http://xxxx.net/request",
                "sAjaxDataProp": "data"
        } );
    } );
  </script>
</body>

当手动访问request.php时,显示如下: [{"uEmail":"xxxx@gmail.com","time":"06-07-2015 7:55 pm","location":"xx.xx.xx.xxx"},{"uEmail":"xxxx@gmail.com","time":"06-07-2015 5:45 pm","location":"xx.xx.xx.xxx"}]

在控制台中,我收到此错误:Uncaught TypeError: Cannot read property 'length' of undefined

这是 DataTable 和控制台的屏幕截图。

【问题讨论】:

  • 我的问题很愚蠢,但我得问一下,我知道你在代码示例中屏蔽了你的 URL,但你有 http://xxxx.net/request 被传递给 dataTables,但你说 request.php 也是如此您传递给 dataTable 的参数必须是 http://xxxx.net/request.php?
  • 查看网络检查器时是否返回了 JSON?另外,是 /request.php 还是 /request?
  • 我的 .htaccess 负责处理无扩展名的 URL,但是我用 xxxx.net/request.php 尝试过,但仍然遇到同样的错误。
  • 网络下的类型为“文档”。如果你愿意,我可以提供截图。我在某处读到 "sAjaxDataProp": "data" 处理返回的内容不是 JSON 的事实。 @山姆

标签: php jquery ajax datatable


【解决方案1】:

根据 DataTables 1.9 文档,sAjaxDataProp 指定返回的 JSON 对象内包含条目的数组的名称。 同时,DataTables 似乎接受条目作为数组,而不是对象。

您的退货格式应为:

{"data": [["xxxx@gmail.com","06-07-2015 7:55 pm","xx.xx.xx.xxx"],["xxxx@gmail.com","06-07-2015 5:45 pm","xx.xx.xx.xxx"]]}

【讨论】:

  • 我删除了sAjaxDataProp,现在我在表格底部看到“显示 1 到 2 个条目,共 2 个条目”。但是我的条目并没有显示在表格本身中。
  • 默认情况下,DataTables 查找的数组是“aaData”;尝试使用上面指定的格式,但使用"aaData" 而不是"data"
  • 以下是出现的警报:“DataTables 警告(表 id = 'example'):从第 0 行的数据源请求未知参数 '0'”
  • 我尝试了你所说的,我在表格底部看到“显示 1 到 2 个条目,共 2 个条目”。但是没有显示条目。我也收到相同的警报消息。
  • 似乎条目被处理为数组而不是对象。您可以尝试使用mysql_fetch_array 代替mysql_fetch_assoc 吗?
猜你喜欢
  • 2023-04-11
  • 2014-04-23
  • 1970-01-01
  • 1970-01-01
  • 2015-05-08
  • 2020-06-02
  • 2020-03-17
  • 1970-01-01
  • 2018-02-12
相关资源
最近更新 更多