I know that it's a common question, but I have a different problem.
I have the following jquery configuration for my datatable:
$( document ).ready(function() {
$('#example').dataTable({
"language": {
"url": "//cdn.datatables.net/plug-ins/9dcbecd42ad/i18n/Spanish.json"
},
"bProcessing": true,
"sAjaxSource": "response.php",
"aoColumns": [
{ mData: 'id' } ,
{ mData: 'name' },
{ mData: 'description' }
//{ mData: 'img', render: getImg },
]
});
});
The generated json from php (that works if loaded directly from file):
{
"sEcho": 1,
"iTotalRecords": 10,
"iTotalDisplayRecords": 10,
"aaData": [{
"id": "1",
"name": "Salsa estilo Casino",
"description": "Descri\u00e7\u00e3o ..."
}, {
"id": "2",
"name": "Salsa estilo Son",
"description": "Descri\u00e7\u00e3o ..."
}, {
"id": "3",
"name": "Salsa LA",
"description": "Descri\u00e7\u00e3o ..."
}, {
"id": "4",
"name": "Salsa NY",
"description": "Descri\u00e7\u00e3o ..."
}, {
"id": "5",
"name": "Bachata",
"description": "Descri\u00e7\u00e3o ..."
}, {
"id": "6",
"name": "Reggaeton",
"description": "Descri\u00e7\u00e3o ..."
}, {
"id": "7",
"name": "Chachacha",
"description": "Descri\u00e7\u00e3o ..."
}, {
"id": "8",
"name": "Rumba",
"description": "Descri\u00e7\u00e3o ..."
}, {
"id": "9",
"name": "Pachanga",
"description": "Descri\u00e7\u00e3o ..."
}, {
"id": "10",
"name": "Ritmos tradicionais cubanos",
"description": "Descri\u00e7\u00e3o ..."
}]
}
My PHP response.php
file:
<?php
// my database classe with CRUDed POJOs.
include_once '../admin/db.php';
// That was from a sample code that worked fine.
$data = array(
array('Name'=>'Descrição ...', 'Empid'=>11, 'Salary'=>101, 'img' => '../img/lais.jpg'),
array('Name'=>'alam', 'Empid'=>1, 'Salary'=>102, 'img' => '../img/lais.jpg'),
array('Name'=>'phpflow', 'Empid'=>21, 'Salary'=>103, 'img' => '../img/lais.jpg')
);
/* Query the database and retrive in $data2[0] the total number of rows
and $data2[1] an array of arrays with the following format (as described above):
array(
array('id' => '1', 'name' => 'name', 'description' => 'Desc ...'),
...
)
*/
$data2 = Turma::fetchAllAssoc();
$results = array(
"sEcho" => 1,
"iTotalRecords" => 10,
"iTotalDisplayRecords" => 10,
"aaData"=>$data2[1]);
header('Content-Type: application/json');
echo json_encode($results);
?>
What am I doing wrong? It seems a bug form me.
Solved my problem.
In PHP, the response.php
was returning only the json content that was shwon in the browser.
The error was: there was some html commented content that seems hidden in `include_once '../admin/db.php';, even the commented html tags.
When I added the line header('Content-Type: application/json');
everything that seems missing was shown in the browser.
Erased everything, even commented html tags to output (echo) only json_encode($results);
. Then, everything worked smoothy.
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.