1

Why does the following XMLHttpRequest return undefined?

function fetchFileList(){
    var request = new XMLHttpRequest();
    request.open("GET","../files.json",false);
    request.onload = function(){
        if(request.status == 200){
            return request.responseText;
        }
    }
    request.send();
}

, but if I console.log() request.responseText, the console outputs what it's supposed to:

if(request.status == 200){
            console.log(request.responseText);
            return request.responseText;
        }

Thanks.

bool3max
  • 2,748
  • 5
  • 28
  • 57
  • 2
    Because `fetchFileList` does not return anything. It does not have a return statement. You probably want to have a look at http://stackoverflow.com/questions/14220321/how-to-return-the-response-from-an-asynchronous-call – Felix Kling Sep 05 '15 at 13:28
  • @Felix Kling ... Thanks, I didn't notice that. The anonymous function acutally reutrns the response text. Thanks. – bool3max Sep 05 '15 at 13:31
  • I think you need a little course: http://stackoverflow.com/a/32163917/1636522. –  Sep 05 '15 at 13:45

0 Answers0