我想从SQL Server获取数据,然后发送到index.php(使用json)以在DataTables中显示数据。但它给我这样的错误
DataTables警告:table id = example - 请求的未知参数' namaptn'第0行。
然后我检查我的index.php,发现json返回此响应
{"数据" :[[{" NoSPTA":" 096342"," TglBerlakuSPTA":" 2017-08-07 23:59:59.000"" Kdptn":" IA045ZN0HG0"" namaptn":" MUNIP KHUSAINI"" TglGawang":" 2017年7月8日 14:55:33"" TglBruto":空," TglGiling":空," TglTara":空,"内托" :" 0.0"" RF":""" Potongan":" 0.0"}]] }
这是我的index.php
的代码<?php
include "db.php";
$obj->tglan=$obj->get_hari();
if (isset($_POST['tanggal2'])) {
$obj->tglan = $_POST['tanggal2'];
}
?>
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript" src="assets/DataTables/media/js/jquery.js"></script>
<script type="text/javascript" src="assets/DataTables/media/js/jquery.dataTables.js"></script>
<link rel="stylesheet" type="text/css" href="assets/css/bootstrap.css">
<link rel="stylesheet" type="text/css" href="assets/DataTables/media/css/jquery.dataTables.css">
<link rel="stylesheet" type="text/css" href="assets/DataTables/media/css/dataTables.bootstrap.css">
<link rel="stylesheet" href="assets/css/bootstrap.min.css"/>
<link rel="stylesheet" href="assets/datepicker/css/bootstrap-datepicker3.css"/>
</head>
<body>
<center>
<h3>Daftar SPTA<br><?php echo $obj->tanggal("D, j M Y",$obj->tglan);?></h3>
</center>
<left>
<h5>   Last refreshed : <?php echo $obj->tanggal("D, j M Y",$obj->tglan)." ".date("H:i:s");?></h5>
</left>
<br/>
<form action="viewLaporanUtama2.php" method="POST">
<div class="form-group" >
<label for="tanggal">   Tanggal</label>
<input type="text" name="tanggal1" class="tanggal" id="myText" required/>
<input type="submit" name="enter" value="Cari" class="btn btn-info btn-sm">
</div>
</form>
<div class="container-fluid">
<div class="table-responsive">
<table border = '0' class="table table-striped table-bordered data" id="tabelSpta">
<thead>
<tr>
<th>Nama Petani</th>
</tr>
</thead>
<tfoot>
<tr>
<th>Nama Petani</th>
</tr>
</tfoot>
</table>
</div>
</div>
</body>
<script type="text/javascript">
$(document).ready(function(){
var tabel = $('#tabelSpta').DataTable({
"ajax": "database.php",
// "sAjaxSource": "data.js",
"order": [[ 0, 'asc' ]],
"columns": [
{ "data": "namaptn" },
],
});
});
</script>
<!-- <script src="js/jquery-3.2.1.min.js"></script> -->
<script src="assets/js/bootstrap.js"></script>
<script src="assets/datepicker/js/bootstrap-datepicker.js"></script>
<script type="text/javascript">
$(document).ready(function () {
$('.tanggal').datepicker({
format: "yyyy-mm-dd",
autoclose:true
});
});
</script>
</html>
这个database.php
<?php
$serverName="192.168.1.250";
$conn = new PDO("sqlsrv:server=$serverName; Database=tebu", "sa", "sukseskan");
$query = "SELECT a.spa AS NoSPTA,
a.tglberlaku as TglBerlakuSPTA,
a.Kdptn,
a.namaptn,
(CONVERT(varchar,b.tgl,103)+' '+ convert( varchar,b.tgl,108))
AS TglGawang,
(CONVERT(varchar,c.tgl,103)+' '+ convert(varchar,c.tgl,108))
AS TglBruto,
(CONVERT(varchar,c.tglgil,103)+' '+ convert(varchar,c.tglgil,108))
AS TglGiling,
(CONVERT(varchar,c.tgltarra,103)+' '+ convert(varchar,c.tgltarra,108))
AS TglTara,
case when tgltarra is not null then
ISNULL(c.bruto, 0) - ISNULL(c.Tara, 0) + 0.001
else ISNULL(c.bruto, 0) end
AS Netto,
isnull(d.RF,'')
as RF, isnull(d.pot,0)
as Potongan
FROM tblSPA a LEFT OUTER JOIN
vtblpos4 d ON a.spa = d.NoSPA LEFT OUTER JOIN
tblbruto c ON a.spa = c.nospa LEFT OUTER JOIN
tblgawang b ON a.spa = b.nospa
where a.tglberlaku>='2017-08-07 00:00:00' and a.tglberlaku<='2017-08-07 23:59:59'
and left(kdptn,5)='IA045'
order by a.spa desc";
// var_dump($query);
$stmt = $conn->prepare($query);
$stmt->execute();
// $hasil = $stmt->fetchAll(PDO::FETCH_ASSOC);
$jsonResult = '{"data" : [ ';
$i = 0;
while ($data=$stmt->fetchAll(PDO::FETCH_ASSOC)) {
if($i != 0){
$jsonResult .=',';
}
$jsonResult .=json_encode($data);
$i++;
}
$jsonResult .= ']}';
// var_dump($jsonResult);
echo $jsonResult;
?>
我不知道我的json有什么问题,我已经输入了类似数据库的参数。我怎么能解决这个问题?