title: How to make a jQuery request to a locally hosted node.js server?
body: <p>I have a simple node.js server saved in a file called server.js, the code for which is below:</p>
  
  <pre><code>var http = require('http');
  
  http.createServer(function (req, res) {
      res.writeHead(200, {'Content-Type': 'text/plain'});
      res.end('Hello World\n');
  }).listen(8010);
  </code></pre>
  
  <p>I am running this locally in command line (with node.js installed) by executing</p>
  
  <pre><code>node server.js
  </code></pre>
  
  <p>If I then access this in my browser (google chrome) with the URL my.local.IP.address:8010 it displays hello world successfully. </p>
  
  <p>I am also creating a webpage that will execute this jQuery as soon as the page is loaded: </p>
  
  <pre><code>&lt;script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js"&gt;&lt;/script&gt;
  &lt;script&gt;    
      $(document).ready(function(){
          $.get("http://my.local.IP.address:8010/", function(){
              alert("Success");
          }).fail(function(err){
              alert("Failed");
              alert(JSON.stringify(err));
          });
      });
  &lt;/script&gt;
  </code></pre>
  
  <p>When I open this html file in my browser it always creates an alert box saying failed followed by this stringified JSON object as the error (I've formatted it so it is easier to read):</p>
  
  <pre><code>{
  "readyState": 0,
  "status" : 0,
  "statusText": "error"
  }
  </code></pre>
  
  <p>This happens no matter how many times I've tried while the node.js server is running locally. I have also tried to make an async XMLHTTP request function that does it myself, and this does not work either.</p>
  
  <p>I know both the jQuery and the XMLHTTP function I made work correctly since if I call the functions on the URL <a href="https://httpbin.org/get" rel="nofollow">https://httpbin.org/get</a> it will return successfully.</p>
  
  <p>Any ideas as to where I have gone wrong?</p>