0

How could I return a value in AJAX XMLHttpRequest?

This way doesn't work (I am sorry, I tried to Google, but I didn't find an answer):

shema = GetDoc("my_file.txt");
alert(shema);

function GetDoc(FileName)  
{
  var xhttp = new XMLHttpRequest();
  xhttp.onreadystatechange = function() 
    {
    if (this.readyState == 4 && this.status == 200) 
      {
      var allText = this.responseText;
      return allText;
      }
    };
  xhttp.open("GET", FileName, true);
  xhttp.send();
}

EDIT: I didn't get the answer in How do I return the response from an asynchronous call? , which I checked before posting.

Community
  • 1
  • 1
brilliant
  • 2,805
  • 11
  • 39
  • 57
  • 1
    Possible duplicate of [How do I return the response from an asynchronous call?](http://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) – Carcigenicate Mar 09 '17 at 18:31
  • @Carcigenicate - I did check that page prior to posting, but I didn't get the answer there. – brilliant Mar 09 '17 at 18:33
  • This is almost definitely a dupe of that, and that question has 17 answers. I'm sure your answer is in fact there. – Carcigenicate Mar 09 '17 at 18:34
  • @Carcigenicate - Can you, please, direct as to which one of those answers is the answer for me? – brilliant Mar 09 '17 at 18:36
  • The first one with 3.2k votes would be a start. You can't expect a catered answer every time. If you're asking this question, it means you don't have a full grasp of how to handle async calls. Once you understand why you can't simply return from a async call, the answer will be obvious. – Carcigenicate Mar 09 '17 at 18:38
  • This first answer goes over pretty much every way to do what you're trying to do. Did you read it? – Carcigenicate Mar 09 '17 at 18:39
  • @Carcigenicate - "**Once you understand why you can't simply return from a async call, the answer will be obvious**" - Thank you. However, I think I need something simpler first in order to get that understanding. I am still "racking my brain" trying to get that understanding from reading that answer. – brilliant Mar 09 '17 at 18:42
  • @Carcigenicate - I just don't get it how come alerting is so simple, but getting the value of what is in the alert is almost impossible (without diving into those promises and stuff). – brilliant Mar 09 '17 at 18:45
  • Async calls are a difficult topic until you work with them a lot. I don't think anyone is going to explain the concept any better than that answer does. Take your time and work through it. The only other option is someone writes a answer catering to your specific code, which might help you more in the short term, but in the long term, it really is important to grasp all the different methods to handle async calls, and understand the logistical differences between sync and async code. – Carcigenicate Mar 09 '17 at 18:48
  • I see. Thank you! – brilliant Mar 09 '17 at 18:50
  • No problem. As a tip, I'd start with something simpler than an AJAX call while you're thinking about the problem. Try messing around with `setTimeout` instead, since it will be much easier to test. Think about why you can't simply return from the callback you give to `setTimeout`. It's the same problem. – Carcigenicate Mar 09 '17 at 18:53

0 Answers0