DataTables warning (table id='displayData'): DataTables warning: JSON data from could not be parsed.

DataTables warning (table id='displayData'): DataTables warning: JSON data from could not be parsed.

dave67dave67 Posts: 0Questions: 0Answers: 0
edited January 2013 in DataTables 1.9
Hello
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:

[code]
<!DOCTYPE html>


Hello jQuery world!


@import "../jquery/datatables/194/media/css/demo_page.css";
@import "../jquery/datatables/194/media/css/demo_table.css";


<!-- -->







id
codepays
CodePostal
Ville
nomadmin




Loading data from server




[/code]

My javascript 26script.js:

[code]
$(document).ready(function() {

$('#displayData').dataTable({

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

});
});

[/code]


I've also tried with following options

[code]
$(document).ready(function() {

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

});
});
[/code]


My php script. (I'm using PDO to access to my mysql database. )

[code]

<?php


$aColumns = array('id', 'codepays', 'cp', 'ville', 'nomadmin1');

$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);
?>

[/code]

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
This discussion has been closed.