2014-06-24 42 views
0

這裏是完整的代碼。這個錯誤似乎是在jsonTesting.php頁面中的變量文獻,歷史和喜劇。它告訴我這些變量沒有被定義。這些變量應該是來自ajax的帖子。請幫助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 – kamesh

回答

0

試試這個,都是唯一的相似,但我實現使用jquery每個堂妹'each'將同時處理對象和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"); 
     } 
    }); 
}); 

如果這是行不通的。使確保從jsonTesting.php你的JSON響應是正確的

+0

檢查JSON響應數據如何檢查從PHP頁面我的JSON響應? – lolo512

+0

直接運行jsonTesting.php或在ajax請求中添加錯誤函數...更新了錯誤函數的代碼 – kamesh

+0

我更新了我的代碼plz help – lolo512