【发布时间】: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 的事实。 @山姆