title: javascript xmlhttprequest always gets status 0
body: <p>I want to query my server triggered by a button click. The function gets called  and the aync request is started. Most of the time it readyState reaches 4, sometimes only 2 - but the status always remains 0.
  The onerror function always triggers, but (as many other have experienced) gives not much info about the problem.</p>
  
  <pre><code>function getServiceVal() {
      var xmlhttp = new XMLHttpRequest();
      xmlhttp.open("GET", "/myservice?param=1"));
  
      xmlhttp.onreadystatechange=function() {
          console.log("--&gt;" + xmlhttp.readyState + ", " + xmlhttp.status)
          if (xmlhttp.readyState==4 &amp;&amp; xmlhttp.status==200) {
              console.log(xmlhttp.responseText.length)
          }
      };
  
      xmlhttp.onerror  = function(e) {
        console.log('There was an error! - ' + xmlhttp + ", " + e + ", ");
      };
  
      xmlhttp.send(null);
  };
  </code></pre>
  
  <p>I also tried the onload function - without further success. Has anybody a idea where the problem could be? CORS shouldn't be it since i use a relative path.
  I am testing on localhost, URL is "<a href="http://localhost:8042/servicetest.html" rel="nofollow noreferrer">http://localhost:8042/servicetest.html</a>"</p>
  
  <p>Output of the snippet always looks like this:</p>
  
  <pre><code>--&gt;2, 0
  --&gt;4, 0
  There was an error! - [object XMLHttpRequest], [object ProgressEvent], 
  --&gt;2, 0
  --&gt;2, 0
  --&gt;4, 0
  There was an error! - [object XMLHttpRequest], [object ProgressEvent], 
  </code></pre>
  
  <p>Thanks a lot for any help &amp; hints!</p>
  
  <p>*****BREAKING NEWS*****</p>
  
  <p>After concentrating on many hours on the javascript code I took a look at the html code invoking it. And really that made the difference. Initially I used a form (because I want to hand over parameters) and invoke the javascript at the submit event.
  Now I changed to a simple button and - tadaa - it works. I don't know why, though:</p>
  
  <pre><code>    &lt;!--form onsubmit="getServiceVal()"&gt;
          &lt;input ref="input"/&gt;
          &lt;button&gt;Get Value&lt;/button&gt;
      &lt;/form--&gt;
      &lt;input type="button" value="This Button!" onclick="getServiceVal();"&gt;
  </code></pre>
  
  <p>This now works, the former in the comment does not. I was not aware that the kind of call can have such an impact.</p>