這裏是完整的代碼。這個錯誤似乎是在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();
}
?>
從jsonTesting.php – kamesh