2017-12-06 13 views
0

나는 이것이 일반적인 질문이라는 것을 알고 있지만 다른 문제점이 있습니다.DataTable - PHP에서 가져온 경우 잘못된 JSON 응답

  • PHP에서 Json 응답을 검색하면이 경고 경고가 표시되고 데이터 테이블에 데이터가로드되지 않습니다.
  • 파일에서 직접 생성 된 Json (PHP에서 검색)을로드하면 데이터 테이블에 데이터가 올바르게로드됩니다.

    $(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 파일

나는 다음과 같은 JQuery와 내 데이터 테이블의 구성을 가지고 :

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

?> 

내가 잘못 했니? 나 한테 버그가 생긴 것 같아.

답변

0

내 문제가 해결되었습니다.

PHP에서 response.php은 브라우저에서 shwon 인 json 콘텐츠 만 반환하고있었습니다.

오류 : 주석 처리 된 HTML 태그조차도`include_once '../admin/db.php'에 숨겨져있는 일부 HTML 주석 처리 된 콘텐츠가 있습니다.

줄을 추가하면 header('Content-Type: application/json'); 누락 된 것으로 보이는 모든 것이 브라우저에 표시됩니다.

모든 내용이 지워졌고, 주석 처리 된 html 태그까지만 출력 (에코) json_encode($results);. 그런 다음 모든 것이 원활하게 이루어졌습니다.