【发布时间】:2014-07-04 17:51:51
【问题描述】:
我正在尝试通过 PHP 和 Ajax 从 MySQL 数据库中获取数据,然后使用 DataTables 将数据显示在表格中。我正在使用 XAMPP 1.8.3
这是我的 html 代码的一部分:
<table id="dataTables-melate" class="table table-striped table-bordered table-hover" cellspacing="0" width="100%">
<thead>
<tr>
<th>Concurso</th>
<th>R1</th>
<th>R2</th>
<th>R3</th>
<th>R4</th>
<th>R5</th>
<th>R6</th>
</tr>
</thead>
<tfoot>
<tr>
<th>Concurso</th>
<th>R1</th>
<th>R2</th>
<th>R3</th>
<th>R4</th>
<th>R5</th>
<th>R6</th>
</tr>
</tfoot>
</table>
这是我的 php 脚本(现在可以编辑并运行):
//default_chart_numbers.php
$loteria='revancha';
$lotto = new Lotto();
$ultimos_resultados=$lotto->last_results($loteria,20);
//echo json_encode($ultimos_resultados);
/*Formatting the output to a non associative array*/
function objectToArray($d)
{
if (is_object($d)) {
// Gets the properties of the given object
// with get_object_vars function
$d = get_object_vars($d);
}
if (is_array($d)) {
/*
* Return array converted to object
* Using __FUNCTION__ (Magic constant)
* for recursive call
*/
return array_map(__FUNCTION__, $d);
} else {
// Return array
return $d;
}
}
$new_array = objectToArray($ultimos_resultados);
//echo '<pre>',print_r($new_array),'</pre>';
$result = array();
echo '[';
foreach ($new_array as $new_array2) {
echo '[';
foreach ($new_array2 AS $value){
echo $value;
if($value!==end($new_array2)){ //referencias: http://stackoverflow.com/a/8780881/1883256
echo',';
}
}
echo ']';//referencias: http://www.mydigitallife.info/how-to-access-php-array-and-multidimensional-nested-arrays-code-syntax/
if($new_array2!==end($new_array)){
echo ',';
}else{ echo '';}
}
echo ']';
这是 PHP 脚本的输出数据的样子(现在有了新的变化):
[[2738,11,12,28,30,50,54], ... ,[2757,32,34,35,36,50,55]]
这里是 jQuery 代码:
<script>
$(document).ready(function() {
$('#dataTables-melate').dataTable({
"bProcessing": true,
"bServerSide": true,
"sAjaxSource": {
"url":"ajax/default_chart_numbers.php",
"type": "POST"
},
"columns":[
{"data": "concurso"},
{"data": "R1"},
{"data": "R2"},
{"data": "R3"},
{"data": "R4"},
{"data": "R5"},
{"data": "R6"}
]
});
} );
</script>
当我加载页面(在 Firefox 中)时,我收到此错误:
DataTables 警告:table id=dataTables-melate - Ajax 错误。
Firebug 也告诉我们这个错误:404 Not Found
我错过了什么? 这么久以来我一直在努力解决这个问题:/
【问题讨论】:
-
我在 chrome 上遇到同样的错误:/
标签: php jquery mysql ajax datatables