简体   繁体   中英

AJAX request returning response text of 'false' instead of desired response text

I have a browserside javascript file that's supposed to be asking the serverside php file for a string with an ajax request. The php file seems to have the correct string, but for some reason the ajax request is returning the string 'false'. This is the php file, named test.php:

    <?php
$dir = "site/uploads";
$a = scandir($dir);
$b = json_encode($a);
echo $b;
?> 

This is the js file:

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();
}

when i open the website in my browser and run load(), the console reads: testfalse testfalse test2false test2false and has no errors. What am I doing wrong here? Thanks!

scandir() in the php-file is probably returning false , because of an error.

https://www.php.net/manual/de/function.scandir.php (Check the return values)

There are multiple possiblities for this to happen:

  • The folder doesn't exist
  • The path is incorrect (Try using absolute paths)
  • The folder isn't accessible, because of missing permissions

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM