簡體   English   中英

DataTable-從PHP檢索時無效的JSON響應

[英]DataTable - Invalid JSON response when retrieved from PHP

我知道這是一個常見問題,但我有另一個問題。

  • 當我從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中, response.php僅返回瀏覽器中顯示的json內容。

錯誤是:有一些html注釋的內容似乎隱藏在`include_once'../admin/db.php';中,甚至包括注釋的html標簽。

當我添加行header('Content-Type: application/json'); 似乎丟失的所有內容都顯示在瀏覽器中。

擦除所有內容,甚至注釋掉html標記以僅輸出(回顯) json_encode($results); 然后,一切工作順利。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM