简体   繁体   English

jQuery datatables mysql php DataTables警告(表id ='displayData'):DataTables警告:无法解析JSON数据

[英]jquery datatables mysql php DataTables warning (table id='displayData'): DataTables warning: JSON data from could not be parsed

I've got the following message error when I load a datatables 加载数据表时出现以下消息错误

DataTables warning (table id='displayData'): DataTables warning: JSON data from could not be parsed. DataTables警告(表id ='displayData'):DataTables警告:无法解析JSON数据。

I've been inspired from http: //datatables.net/examples/server_side/server_side.html 我的灵感来自http://datatables.net/examples/server_side/server_side.html

My html code: 我的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>

My javascript 26script.js: 我的JavaScript 26script.js:

$(document).ready(function() {

    $('#displayData').dataTable({

        "sAjaxSource" : "../data/json/261arrays.php",

    });
});

I've also tried with following options 我也尝试了以下选项

$(document).ready(function() {

    $('#displayData').dataTable({
                "bProcessing": true,
        "bServerSide": true,
        "sAjaxSource" : "../data/json/261arrays.php",

    });
});

My php script. 我的PHP脚本。 (I'm using PDO to access to my mysql database. ) (我正在使用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);

?>

When I log my result array I get a array with following structure : 当我记录结果数组时,我得到一个具有以下结构的数组:

{
    "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"
        ]
    ]
}

I checked this array in http: //jsonlint.com/ and it's well formed json array 我在http://jsonlint.com/中检查了此数组,它的格式正确

When I copy this array and paste in a file with a extension file.json for example and if I modify my script, my datatable is loaded and I can see the result on screen. 例如,当我复制此数组并将其粘贴到带有扩展名file.json的文件中时,如果我修改脚本,则会加载数据表,并且可以在屏幕上看到结果。

I've also try http: //debug.datatables.net/ bug to look at an eventual issue in my array but I don't understand how it works. 我也尝试过http://debug.datatables.net/ bug来查看数组中的最终问题,但我不知道它是如何工作的。

What have I modify to display at screen, my json array achieved by php script ? 我有什么修改要显示在屏幕上,通过php脚本实现的json数组?

Thanks in advance 提前致谢

Yes it's seems the expected result returned by php cannot be dealed with datatable library. 是的,看来php返回的预期结果无法用datatable库处理。

Array pattern in json file couldn't be the same than json array pattern generated by php script. json文件中的数组模式不能与php脚本生成的json数组模式相同。 So if we follow what happens in example referenced to http://www.datatables.net/examples/data_sources/server_side.html 因此,如果我们遵循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);

We get the following structure for json array generated by php script 对于由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"
        ]
    ]
}

In datatables.net website, data are fetched with "mysql_query" (I don't like because php deprecate mysql interface). 在datatables.net网站上,使用“ mysql_query”获取数据(我不喜欢,因为php弃用了mysql接口)。 php advice mysqli interface. PHP的建议mysqli界面。 But for other database probably we need to use PDO interface. 但是对于其他数据库,我们可能需要使用PDO接口。 To get same result with same pattern by using PDO interface, it's necessary to write : fetch(PDO::FETCH_ASSOC) 要使用PDO接口以相同的模式获得相同的结果,必须编写:fetch(PDO :: FETCH_ASSOC)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 DataTables警告:无法解析来自服务器的JSON数据。 这是由JSON格式错误引起的 - DataTables warning: JSON data from server could not be parsed. This is caused by a JSON formatting error 数据表警告表 id=datatables-example - 无效的 json 响应 - Datatables warning table id=datatables-example - invalid json response 数据表,无法解析来自服务器的JSON数据 - datatables, JSON data from server could not be parsed DataTables警告:表格ID = staff_data-无效的JSON响应 - DataTables warning: table id=staff_data - Invalid JSON response php DataTables中的Ajax警告:表格ID = example-无效的JSON响应 - Ajax from php DataTables warning:table id=example - Invalid JSON response DataTables警告:表格ID = {id}-无效的JSON响应 - DataTables warning: table id={id} - Invalid JSON response DataTables警告:表格ID = DataTables_Table_0-Ajax错误 - DataTables warning: table id=DataTables_Table_0 - Ajax error DataTables警告:table id = example - 无效的JSON响应 - DataTables warning: table id=example - Invalid JSON response DataTables警告:table id = dataTables - Ajax错误。 404未找到 - DataTables warning: table id=dataTables - Ajax error. 404 Not Found 数据表,数据库服务器端处理和“数据表警告(表ID =&#39;示例&#39;)” - DataTables, database serverside processing and “DataTables warning (table id = 'example')”
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM