我收到一個奇怪的錯誤,我不確定它爲什麼會發生。我試圖發送多個值給我的ajax調用,導致undefined
。用PHP解析錯誤json_encode
我試圖調試它,我意識到我的PHP得到了一個解析錯誤,我的json_encode
。原因似乎是隨着多重價值的傳遞。任何人都可以解釋爲什麼這樣嗎?
<?php
ini_set('display_errors', 1);
error_reporting(E_ALL);
$files = glob("images/*.*");
for ($i=0; $i<count($files); $i++) {
$image = $files[$i];
}
echo json_encode("array_of_images" => $files, "size_of_array" => sizeof($files));
?>
更新:Ajax代碼
<script>
$.ajax({ //create an ajax request to load_page.php
type: "GET",
url: "img.php",
dataType: "html", //expect html to be returned
success: function(response){
alert(response.array_of_images);
alert(response.size_of_array);
},
error:function (xhr, ajaxOptions, thrownError){
// alert(thrownError);
}
});
</script>
謝謝,但爲什麼我仍然得到一個未定義以下Ajax響應: 成功:函數(響應){ \t警報(response.array_of_images); \t alert(response.size_of_array); \t}, – nTuply
嘗試'console.log(response)',然後提醒它找出它究竟包含什麼。 – aynber
未在console.log中定義。如果我只說'json_encode($ files)'它可以工作。 – nTuply