简体   繁体   中英

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.

I've been inspired from http: //datatables.net/examples/server_side/server_side.html

My html code:

<!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:

$(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. (I'm using PDO to access to my mysql database. )

<?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

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.

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.

What have I modify to display at screen, my json array achieved by php script ?

Thanks in advance

Yes it's seems the expected result returned by php cannot be dealed with datatable library.

Array pattern in json file couldn't be the same than json array pattern generated by php script. So if we follow what happens in example referenced to 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

{
    "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). php advice mysqli interface. But for other database probably we need to use PDO interface. To get same result with same pattern by using PDO interface, it's necessary to write : fetch(PDO::FETCH_ASSOC)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM