Php DataTables 1.10从json解析aadata在HTML5上失败
我目前正在使用HTML5,所以我需要使用DataTabels 1.10 问题是ajaxsouce无法解析我所拥有的php文件中的json。 事实上,它工作得很好,在我的DataTables 1.9.4和HTML4项目中仍然工作得很好,但不知何故,我在DataTables 1.10和HTML5中使用它失败了 这就是我如何称呼我的ajax:Php DataTables 1.10从json解析aadata在HTML5上失败,php,json,html,datatables,Php,Json,Html,Datatables,我目前正在使用HTML5,所以我需要使用DataTabels 1.10 问题是ajaxsouce无法解析我所拥有的php文件中的json。 事实上,它工作得很好,在我的DataTables 1.9.4和HTML4项目中仍然工作得很好,但不知何故,我在DataTables 1.10和HTML5中使用它失败了 这就是我如何称呼我的ajax: $(document).ready(function() { $('#example').dataTable( { "ajax": "viewobat1
$(document).ready(function() {
$('#example').dataTable( {
"ajax": "viewobat1.php"
} );
})
我已经改为“sAjaxSource”:“viewobat1.php”
,但也不起作用
这就是我在viewobat.php
中获取json的方法
$sql="SELECT * FROM obat";
$hasil=mysql_query($sql);
print("{\"aaData\":[");
$tambahan="\n";
$no=0;
while($cetak=mysql_fetch_array($hasil))
{$no++;
$no;
$idobat=$cetak[0];
$tanggalmasuk=$cetak[1];
$namaobat=$cetak[2];
$jenis=$cetak[3];
$macam=$cetak[4];
$keterangan=$cetak[5];
$stok=$cetak[6];
print($tambahan."[\"$no\","."\"$idobat\","."\"$tanggalmasuk\","."\"$namaobat\","."\"$jenis\","."\"$macam\",
"."\"$keterangan\","."\"$stok\]");
$tambahan=",\n";
}
print("\n]}\n");
?>
我看我的json没有任何问题,PHP正在返回一个有效的json数组
但数据不会显示在表格上
我错过什么了吗
~已编辑~
我已经改变了从datatables.net获取服务器端教程所需数据的方式-->
我的代码是这样的
<?php
error_reporting(0);
$table = 'obat';
$primaryKey = 'idobat';
$columns = array(
array( 'db' => 'idobat', 'dt' => 0 ),
array( 'db' => 'tanggalmasuk', 'dt' => 1 ),
array( 'db' => 'namaobat', 'dt' => 2 ),
array( 'db' => 'jenis', 'dt' => 3 ),
array( 'db' => 'macam', 'dt' => 4 ),
array( 'db' => 'keterangan', 'dt' => 5 ),
array( 'db' => 'stok', 'dt' => 6 ),
);
$sql_details = array(
'user' => 'root',
'pass' => '',
'db' => 'medical',
'host' => 'localhost'
);
require( 'js\DataTables-1.10.2\examples\server_side\scripts\ssp.class.php' );
echo json_encode(
SSP::simple( $_GET, $sql_details, $table, $primaryKey, $columns )
);
是的。。。我找到了我的答案(这是个奇怪的答案)
刚把我的脚本改成
-->
而且它做了所有的把戏。。。这很奇怪,不是吗。。。?我知道
我不知道为什么我应该使用需要http的CDN,并且使用PATH\u TO\u FOLDER方法从我自己的服务器调用它失败
但不管怎样,它是有效的,
问题-->
department.php(服务器端)
index.php(客户端)
部门ID
部门名称
部门ID
部门名称
$(文档).ready(函数(){
变量表=$(“#示例”)。数据表({
阿贾克斯:{
url:'department.php',
dataSrc:'aaData',
方法:'POST'
},
栏目:[
{数据:'dept_id'},
{数据:'部门名称'}
],
});
这个代码对我有用。。。。!
使用dataTable 1.10,jQuery 2.1.0
<script type="text/javascript" src="http://code.jquery.com/jquery-1.11.1.min.js"></script>
<script type="text/javascript" src="http://cdn.datatables.net/1.10.2/js/jquery.dataTables.min.js"></script>
<?php
$qry="select dept_id,dept_name from tb_department ";
$result=mysqli_query($connection,$qry);
$data=array();
$i=0;
while($row=mysqli_fetch_assoc($result)){
$data[] = $row;
}
$response = array(
'aaData' => $data,
'iTotalRecords' => count($data),
'iTotalDisplayRecords' => count($data)
);
echo json_encode($response);
?>
<table id="example" class="table table-bordered table-striped table-hover">
<thead>
<tr>
<th>Department ID </th>
<th>Department Name</th>
</tr>
</thead>
<tfoot>
<tr>
<th>Department ID </th>
<th>Department Name</th>
</tr>
</tfoot>
</table>
<script >
$(document).ready(function(){
var table= $('#example').DataTable( {
ajax: {
url: 'department.php',
dataSrc: 'aaData',
method:'POST'
},
columns: [
{ data: 'dept_id' },
{ data: 'dept_name' }
],
});
</script>