title: Returning AJAX responseText as string
body: <p>In my code I attempt to use AJAX to fetch the content of a template file and return it as a string, which I alert. This results in <code>undefined</code>, however if instead of <code>return xmlhttp.responseText</code> I do <code>alert(xmlhttp.responseText)</code> I get the result I'm after.</p>
  
  <p>Can I return the AJAX content as a string outside the function??</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>alert(getFile());
  
  function getFile() {
  
    var xmlhttp = new XMLHttpRequest();
    xmlhttp.onreadystatechange = function stateChanged() {
      if (xmlhttp.readyState == 4)
        return xmlhttp.responseText;
    }
  
    xmlhttp.open("POST","/template.html", true);
    xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    xmlhttp.send();
  }</code></pre>
  </div>
  </div>
  </p>