[英]AJAX request returning response text of 'false' instead of desired response text
我有一個瀏覽器端 javascript 文件,它應該向服務器端 php 文件詢問帶有 ajax 請求的字符串。 php 文件似乎具有正確的字符串,但由於某種原因,ajax 請求返回字符串“假”。 這是 php 文件,名為 test.php:
<?php
$dir = "site/uploads";
$a = scandir($dir);
$b = json_encode($a);
echo $b;
?>
這是.js文件:
function load(){
var xmlhttp = new XMLHttpRequest();
xmlhttp.responseType = "text";
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
console.log('test' + xmlhttp.responseText);
console.log('test' + JSON.parse(xmlhttp.responseText));
}
};
xmlhttp.open("GET", "test.php", 'false');
xmlhttp.send();
var xmlhttp2 = new XMLHttpRequest();
xmlhttp2.responseType = "text";
xmlhttp2.onreadystatechange = function() {
if (xmlhttp2.readyState == 4 && xmlhttp2.status == 200) {
console.log('test2' + xmlhttp2.responseText);
console.log('test2' + JSON.parse(xmlhttp2.responseText));
}
};
xmlhttp2.open("GET", "test2.php", 'false');
xmlhttp2.send();
}
當我在瀏覽器中打開網站並運行 load() 時,控制台顯示:testfalse testfalse test2false test2false 並且沒有錯誤。 我在這里做錯了什么? 謝謝!
由於錯誤,php 文件中的scandir()
可能返回false
。
https://www.php.net/manual/de/function.scandir.php (檢查返回值)
發生這種情況有多種可能性:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.