title: Can't send XMLHttpRequest/XDomainRequest in IE8, fails on xhr.send()
body: <p>I've looked through answers but can't seem to find one that fixes this particular problem. Also a <a href="https://stackoverflow.com/questions/43855467/xdomainrequest-unspecified-error-in-ie8">similar question</a> is still unanswered.</p>
  
  <p>I am using IE8 to send a XMLHttpRequest but it never gets sent. IE8 supports XMLHttpRequest objects, so why isn't it sending? (I can't use jQuery for this.)</p>
  
  <p>My code:</p>
  
  <pre><code>window.onload = function() {
      // Create the HTTP object
      var xmlhttp;
      if (window.XMLHttpRequest) {
          xmlhttp = new XMLHttpRequest();
      } else {
          alert("couldn't load data");
      }
  
      var dataUrl = "./data2.json";
      alert("sending");
      xmlhttp.open("GET", dataUrl, true);
      alert("sent");
  
      xmlhttp.onreadystatechange = function() {
          //ready?
          if (xmlhttp.readyState != 4)
              return false;
  
          //get status:
          var status = xmlhttp.status;
  
          //maybe not successful?
          if (status != 200) {
              alert("AJAX: server status " + status);
              return false;
          }
  
          //Got result. All is good.
          alert(xmlhttp.responseText);
  
          return true;
      }
      xmlhttp.send(null);
  }
  </code></pre>
  
  <p>The <code>alert('sent');</code> never gets called, but <code>alert('sending');</code> does. How can I make this xmlhttp request work?</p>
  
  <p>Edit: I've updated my code to include XDomainRequest for IE8. This gets me past the xmlhttp.open part, but now I'm getting an "Unspecified error" on the xmlhttp.send part. </p>
  
  <p>edited code (Note: I changed my variable name from <code>xmlhttp</code> to <code>xhr</code>.):</p>
  
  <pre><code>window.onload = function() {
      // Create the HTTP object
      var xhr;
      if (window.XMLHttpRequest) {
          xhr = new XMLHttpRequest();
      } else {
          alert("couldn't load data");
      }
  
      var dataUrl = "./data2.json";
      alert("sending");
  
      if ("withCredentials" in xhr) {
          // XHR for Chrome/Firefox/Safari
          xhr.open('get', dataUrl, true);
      }else if (typeof XDomainRequest != "undefined") {
          // XDomainRequest for IE.
          xhr = new XDomainRequest();
          xhr.open('get', dataUrl);
          alert("opened");
      } else{
          alert('CORS not supported');
      };
  
      xhr.onprogress = function () { };
      xhr.ontimeout = function () { };
      xhr.onerror = function () { alert('error'); };
      xhr.onload = function() {
       alert(xhr.responseText);
      }
      setTimeout(function () {xhr.send();}, 0);
  }
  </code></pre>
  
  <p>alert('error'); gets called. </p>