title: Returning the XML received on readystatechange - JavaScript
body: <p>This is my first post here on Stack. I am trying to make a Get request which is succeeding but I am having trouble getting the responseXML into a variable for processing. I think I am supposed to be using a callback function, but I still can't get it quite right. I am hopeful that somebody can point me in the correct direction. Code below.</p>
  
  <pre><code>    &lt;script type="text/javascript"&gt;
          function buildOptions() {
              var data = null;
  
  
  
              /*xhr.addEventListener("readystatechange", function () {
              if (this.readyState === 4) {
                  console.log(this.responseText);
                  callback.call(xhr.responseXML);
                }
              });*/ //This code block worked, but I couldn't figure out how to get the result back
  
              getXML = function(callback) {
                  var xhr = new XMLHttpRequest();
                  xhr.withCredentials = true;
                  xhr.onreadystatechange = function() {
                      if (xhr.readyState == 4 &amp;&amp; xhr.status == 200) {
                          callback(xhr.responseText);
                      }
                      xhr.open("GET", "http://URLRemoved");
                      xhr.setRequestHeader("authorization", "StringRemoved");
                      xhr.setRequestHeader("cache-control", "no-cache");
                      xhr.setRequestHeader("postman-token", "TokenRemoved");
                      xhr.send();
                  }
              }
  
              function XMLCallBack(data) {
                  alert(data); // These two functions were the most recent attempt but I'm still missing something
              }
  
              xmlDoc = getXML(XMLCallBack); // this is supposed to start the processing of the returned XML
              console.log(xmlDoc);
              var campaignName = xmlDoc.getElementsByTagName('self')[0]; //XMLDoc contains a null variable when I get to this line
              console.log(campaignName);
              var campaigns = ["","Freshman Campaign","Sophomore Campaign","Junior Campaign","Senior Campaign"]; //Code from here and below will be changing slightly once I can get XMLDoc to be correct
              var sel = document.getElementById('campaignList');
              for(var i = 0; i &lt; campaigns.length; i++) {
              var opt = document.createElement('option');
              opt.innerHTML = campaigns[i];
              opt.value = campaigns[i];
              sel.appendChild(opt);
              }
          }
      &lt;/script&gt;
  </code></pre>