0

I have been through countless SO questions about this subject, but none of them worked. The closest I could find was this one.

The problem of the OP was that he did not call the function, so it could not work. I am however calling the fonction, with a simple console.log() as follows:

function loadXMLDoc()
          {   
              var textfile;
              if (window.XMLHttpRequest)
              { 
                  textfile = new XMLHttpRequest(); 
              }
              textfile.onreadystatechange = function ()
              {   
                  if (textfile.readyState == 4 && textfile.status == 200)
                  { 
                      content = textfile.responseText; 
                  }
              }
              textfile.open("GET", "data/Sample.json", true);
              textfile.send();
          }

//var jsonString = loadXMLDoc();
console.log(loadXMLDoc());

In this example, console.log(LoadXMLDoc()) return undefined and the OP marked the question as solved. I am very confused, and all I want is to count the number of occurences of a string in this json file.

Obviously, there is no return, so that might be an issue, and I don't know what .send() does.

I tried adding those lines at some point: return textfile.send(); return textfile.responseText; and even return textfile.open("GET", "data/Sample.json", true); but none would work.

I would like to either get this snipet to work, or come up with another one that will count occurences in a json file (the formatting of the json has no importance as long as I can count the occurrences, as I will not use the file for anything else).

Community
  • 1
  • 1
MorganFR
  • 331
  • 3
  • 19
  • 2
    because `loadXMLDoc` does not return anything, and you can not return from an asynchronous method, so you need to learn how to use callbacks. – epascarello Nov 08 '16 at 12:46
  • http://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call – epascarello Nov 08 '16 at 12:48

0 Answers0