我收到一个奇怪的错误,我不确定它为什么会发生。我试图发送多个值给我的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