![](/img/trans.png)
[英]DataTables warning: JSON data from server could not be parsed. This is caused by a JSON formatting error
[英]jquery datatables mysql php DataTables warning (table id='displayData'): DataTables warning: JSON data from could not be parsed
加載數據表時出現以下消息錯誤
DataTables警告(表id ='displayData'):DataTables警告:無法解析JSON數據。
我的靈感來自http://datatables.net/examples/server_side/server_side.html
我的html代碼:
<!DOCTYPE html>
<html>
<head>
<title>Hello jQuery world!</title>
<link rel="stylesheet" type="text/css" href="../jquery/css/192/ui-lightness/jquery-ui-1.9.2.custom.css">
<style type="text/css">
@import "../jquery/datatables/194/media/css/demo_page.css";
@import "../jquery/datatables/194/media/css/demo_table.css";
</style>
<script type="text/javascript" src="../jquery/js/183/jquery-1.8.3.js"></script>
<!-- <script type="text/javascript" src="../jquery/js/192ui/jquery-ui-1.9.2.custom.js"></script> -->
<script type="text/javascript" charset="utf-8" src="../jquery/datatables/194/media/js/jquery.dataTables.js"></script>
<script type="text/javascript" src="../js/26script.js"></script>
</head>
<body>
<table cellpadding="0" cellspacing="0" border="0" class="display" id="displayData">
<thead>
<tr>
<th align="left">id</th>
<th align="left">codepays</th>
<th align="left">CodePostal</th>
<th align="left">Ville</th>
<th align="left">nomadmin</th>
</tr>
</thead>
<tbody>
<tr>
<td colspan="5" class="dataTables_empty">Loading data from server</td>
</tr>
</tbody>
</table>
</html>
我的JavaScript 26script.js:
$(document).ready(function() {
$('#displayData').dataTable({
"sAjaxSource" : "../data/json/261arrays.php",
});
});
我也嘗試了以下選項
$(document).ready(function() {
$('#displayData').dataTable({
"bProcessing": true,
"bServerSide": true,
"sAjaxSource" : "../data/json/261arrays.php",
});
});
我的PHP腳本。 (我正在使用PDO訪問我的mysql數據庫。)
<?php
$aColumns = array('id', 'codepays', 'CodePostal', 'Ville', 'nomadmin');
$dsn = 'mysql:host=localhost;dbname=';
$db = 'fde_travel';
$username = 'root';
$password = 'root';
//Initialisation de la liste
$list = array();
$listt = array();
$sTable = "tvl_cp";
$sIndexColumn = "id";
//Connexion MySQL
$options = array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8", PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION);
try {
$db = new PDO($dsn . $db, $username, $password, $options);
} catch (Exception $ex) {
echo $ex -> getMessage();
}
//Construction de la requete
$sQuery = "SELECT id id ,codepays , CP CodePostal, VILLE Ville, nomadmin1 nomadmin FROM tvl_cp limit 100";
$query = $db -> prepare($sQuery);
$query -> execute();
$ar = array();
$num = 0;
while ($listt = $query -> fetch(PDO::FETCH_NUM, PDO::FETCH_ORI_NEXT)) {
$ar[$num] = $listt;
$num = $num + 1;
}
$oututt['aaData'] = $ar;
//header('Content-type: application/json');
echo json_encode($oututt);
?>
當我記錄結果數組時,我得到一個具有以下結構的數組:
{
"aaData": [
[
"1",
"BE",
"1000",
"Bruxelles",
"Bruxelles-Capitale"
],
[
"2",
"BE",
"1005",
"Conseil Region Bruxelles-Capitale",
"Bruxelles-Capitale"
],
[
"3",
"BE",
"1006",
"Raad Vlaamse Gemeenschapscommissie",
"Bruxelles-Capitale"
],
[
"4",
"BE",
"1007",
"Ass. Commiss. Communau. française",
"Bruxelles-Capitale"
],
[
"100",
"BE",
"1700",
"Dilbeek Sint-Ulriks-Kapelle",
"Vlaanderen"
]
]
}
我在http://jsonlint.com/中檢查了此數組,它的格式正確
例如,當我復制此數組並將其粘貼到帶有擴展名file.json的文件中時,如果我修改腳本,則會加載數據表,並且可以在屏幕上看到結果。
我也嘗試過http://debug.datatables.net/ bug來查看數組中的最終問題,但我不知道它是如何工作的。
我有什么修改要顯示在屏幕上,通過php腳本實現的json數組?
提前致謝
是的,看來php返回的預期結果無法用datatable庫處理。
json文件中的數組模式不能與php腳本生成的json數組模式相同。 因此,如果我們遵循http://www.datatables.net/examples/data_sources/server_side.html引用的示例中發生的情況
$output = array(
"sEcho" => 1,
"iTotalRecords" => 100,
"iTotalDisplayRecords" => 100,
"aaData" => array()
);
while ($aRow = $query -> fetch(PDO::FETCH_ASSOC)) {
$row = array();
for ( $i=0 ; $i<count($aColumns) ; $i++ ) {
if ( $aColumns[$i] != ' ' ) {
/* General output */
$row[] = $aRow[ $aColumns[$i] ];
}
}
$output['aaData'][] = $row;
}
echo json_encode($output);
對於由php腳本生成的json數組,我們得到以下結構
{
"sEcho": 1,
"iTotalRecords": 100,
"iTotalDisplayRecords": 100,
"aaData": [
[
"1",
"BE",
"1000",
"Bruxelles",
"Bruxelles-Capitale"
],
[
"2",
"BE",
"1005",
"Conseil Region Bruxelles-Capitale",
"Bruxelles-Capitale"
],
[
"3",
"BE",
"1006",
"Raad Vlaamse Gemeenschapscommissie",
"Bruxelles-Capitale"
],
[
"4",
"BE",
"1007",
"Ass. Commiss. Communau. française",
"Bruxelles-Capitale"
],
[
"100",
"BE",
"1700",
"Dilbeek Sint-Ulriks-Kapelle",
"Vlaanderen"
]
]
}
在datatables.net網站上,使用“ mysql_query”獲取數據(我不喜歡,因為php棄用了mysql接口)。 PHP的建議mysqli界面。 但是對於其他數據庫,我們可能需要使用PDO接口。 要使用PDO接口以相同的模式獲得相同的結果,必須編寫:fetch(PDO :: FETCH_ASSOC)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.