title: jQuery AJAX to node server throws net::ERR_CONNECTION_REFUSED
body: <p>I have disabled my firewall (ufw on Ubuntu 15.04 server). This seems to be the most common cause of this problem.</p>
  
  <p>Additionally some people have problems using AJAX, so I tried with both ajax and an https POST request. My code is as follows:</p>
  
  <p>jQuery making the request:</p>
  
  <pre><code>testData = {
      email     : address,
      subject   : subject,
      body      : body,
      meetingId : meetingId
  };
  
  testData = JSON.stringify(testData);
  
  $.ajax({
      url: "https://localhost:3000/sendICSInvite",
      contentType: "application/json; charset=utf-8",
      data: testData,
      type: "POST",
      success: function(result) {
          console.log("request might have worked");
          console.log(result);
      },
      error: function (err) {
          console.log(err);
      }
  }); 
  </code></pre>
  
  <p>To the route: </p>
  
  <pre><code>var api = require("../controllers/main.js");
  
  //API ROUTES
  module.exports = function(app){
    app.post('/sendICSInvite', api.sendEmail);
  };
  </code></pre>
  
  <p>The controller:</p>
  
  <pre><code>module.exports.sendEmail = function (req, res) {
    console.log("sending Email (probably not actually)");
  });
  </code></pre>
  
  <p>This can be called with postman and works flawlessly. When I do a call from jQuery, however, it fails and returns the error:</p>
  
  <pre><code>OPTIONS https://localhost:3000/sendICSInvite?email=myemail%40myprovider.com&amp;subject=TE…+emails&amp;body=Just+testing+the+function+for+autmoated+email&amp;meetingId=33033 net::ERR_CONNECTION_REFUSEDsend 
  @ jquery.js:4m.extend.ajax
  @ jquery.js:4sendEmail 
  @ caseman.js:706(anonymous function) 
  @ VM4606:2InjectedScript._evaluateOn 
  @ VM4491:875InjectedScript._evaluateAndWrap 
  @ VM4491:808InjectedScript.evaluate 
  @ VM4491:664
  </code></pre>
  
  <p>Seems to be related to the OPTIONS request (I don't know where this is sent, I sepcifiy POST type in my ajax), which lead me to this proposed solution in my node server:</p>
  
  <pre><code>app.use(function(req, res, next) {  
    res.header('Access-Control-Allow-Origin', "*");  
    res.header('Access-Control-Allow-Methods',"GET,PUT,POST,DELETE,OPTIONS");
    res.header('Access-Control-Allow-Headers', "*");  
    next(); 
  });
  </code></pre>
  
  <p>But even with this middleware function the error stays the same. The node server is running on port 3000 on a virtual box using VMware Workstation and running Ubuntu 15.04 server edition. The website containing the jQuery that I want to issue this request is served through a Perl script on port 80.</p>