DataTable - 从PHP检索时无效的JSON响应

时间:2017-12-06 17:26:54

标签: php jquery json datatable

我知道这是一个常见的问题,但我有一个不同的问题。

  • 当我从PHP检索Json响应时,它会抛出此警告警告,并且不会将数据加载到数据表中。
  • 当我直接从文件加载生成的Json(从PHP检索)时,数据表正确加载数据。

我的数据表有以下jquery配置:

$( 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 },
        ]
      });
});

从php生成的json(如果直接从文件加载,则有效):

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

我的PHP response.php文件:

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

?>

我做错了什么?这似乎是我的一个错误。

1 个答案:

答案 0 :(得分:0)

解决了我的问题。

在PHP中,response.php仅返回浏览器中的json内容。

错误是:有一些html注释内容似乎隐藏在`include_once'../ admin / db.php';,甚至是注释的html标签。

当我添加行header('Content-Type: application/json');时,浏览器中会显示所有遗漏的内容。

删除了所有内容,甚至只将html标记注释为输出(echo)json_encode($results);。然后,一切顺利。