2014-06-24 1 views
0

여기에 전체 코드가 나와 있습니다. 오류는 jsonTesting.php 페이지에 문학, 역사 및 코미디 변수가있는 것으로 보입니다.이 변수는 정의되어 있지 않습니다. 이러한 변수는 아약스의 게시물이어야합니다. 도와주세요 asap.here 내 PHP 페이지 (jsonTesting.php)입니다 :json에서 ajax 및 php를 사용하는 구문 오류

<?php 
$library= 
'{"closets":[ 
      {"id":"001","theme":"literature","shelves": 
                  { 
                  "books": [{"id":"b1","genre":"english Literature"}, 
                    {"id":"b2","genre":"arabic literature"}, 
                    {"id":"b3","genre":"french literature"} 
                     ] 
                  } 
      }, 
      {"id":"002","theme":"comedy","shelves":{ 
                 "books":[{"id":"b11","genre":"english comedy"}, 
                   {"id":"b22","genre":"arabic comedy"}, 
                   {"id":"b33","genre":"french comedy"} 
                   ] 
                } 
      }, 
      {"id":"003","theme":"history","shelves":{ 
                 "books":[{"id":"b111","genre":"english history"}, 
                   {"id":"b222","genre":"arabic history"}, 
                   {"id":"b333","genre":"french history"} 
                   ] 
                 } 
      } 
      ] 

}'; 
//isset is used to make sure that we are reciving the post from the ajax 
if(isset($_POST["literature"])) 
$literature=$_POST["literature"]; 

if(isset($_POST["comedy"])) 
$comedy=$_POST["comedy"]; 

if(isset($_POST["history"])) 
$history=$_POST["history"]; 

$library = json_decode($library, true); 

$literatureBooks = $library['closets'][0]['shelves']['books']; 
$comedyBooks= $library['closets'][1]['shelves']['books']; 
$historyBooks= $library['closets'][2]['shelves']['books']; 

if($literature) 
{ 
echo json_encode($literatureBooks); 
exit(); 
} 

if ($comedy){ 

echo json_encode($comedyBooks); 
exit(); 
} 
if($history){ 
echo json_encode($historyBooks); 
exit(); 
} 
?> 
+0

jsonTesting.php에서 json 응답 데이터를 확인하십시오. – kamesh

답변

0

둘은 비슷하지만 두 객체와 JSON 배열

$.ajax({ 
     url:"jsonTesting.php", 
     data: {literature:'1'}, 
     type:'POST', 
     success:function(data){ 
     if(data) 
     { 
     $.each(data,function(key,value){ 
      $("#books").append('<span>literature Books are'+value.id+'</span><br/>'); 
     } 
     }, 
     error:function(){ 
     alert("Error in connection"); 
     } 
    }); 
}); 
을 처리 할 'each' 사촌 각 jQuery를 사용하여 달성이 시도

이것이 작동하지 않는 경우 jsonTesting.php에서 json 응답이 올바른지 확인하십시오.

+0

PHP 페이지에서 json 응답을 어떻게 확인할 수 있습니까? – lolo512

+0

jsonTesting.php를 직접 실행하거나 아약스 요청에 오류 함수를 추가하십시오 ... 오류 코드 – kamesh

+0

으로 업데이트되었습니다. 내 코드 plz 도움말을 업데이트했습니다. – lolo512

관련 문제