【问题标题】:DataTable - Invalid JSON response when retrieved from PHPDataTable - 从 PHP 检索时 JSON 响应无效
【发布时间】:2017-12-06 17:26:53
【问题描述】:

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

  • 当我从 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);

?>

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

【问题讨论】:

    标签: php jquery json datatable


    【解决方案1】:

    解决了我的问题。

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

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

    当我添加header('Content-Type: application/json'); 行时,似乎缺少的所有内容都显示在浏览器中。

    删除所有内容,甚至注释 html 标签以仅输出(回显)json_encode($results);。然后,一切顺利。

    【讨论】:

      猜你喜欢
      • 2017-05-29
      • 2019-02-17
      • 1970-01-01
      • 1970-01-01
      • 2015-01-14
      • 1970-01-01
      • 2023-03-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多