我有一个浏览器端的javascript文件,它应该向服务器端的php文件请求一个带有ajax请求的字符串。php文件似乎有正确的字符串,但由于某些原因,ajax请求返回字符串'false‘。这是名为test.php的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并且没有错误。我在这里做错了什么?谢谢!
发布于 2020-05-26 02:46:12
由于错误,php文件中的scandir()可能会返回false。
https://www.php.net/manual/de/function.scandir.php (检查返回值)
发生这种情况的可能性有多种:
https://stackoverflow.com/questions/62008519
复制相似问题