1

I have disabled my firewall (ufw on Ubuntu 15.04 server). This seems to be the most common cause of this problem.

Additionally some people have problems using AJAX, so I tried with both ajax and an https POST request. My code is as follows:

jQuery making the request:

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);
    }
}); 

To the route:

var api = require("../controllers/main.js");

//API ROUTES
module.exports = function(app){
  app.post('/sendICSInvite', api.sendEmail);
};

The controller:

module.exports.sendEmail = function (req, res) {
  console.log("sending Email (probably not actually)");
});

This can be called with postman and works flawlessly. When I do a call from jQuery, however, it fails and returns the error:

OPTIONS https://localhost:3000/sendICSInvite?email=myemail%40myprovider.com&subject=TE…+emails&body=Just+testing+the+function+for+autmoated+email&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

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:

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(); 
});

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.

awimley
  • 692
  • 1
  • 9
  • 29

1 Answers1

0

data: JSON.stringify(testdata),

That's probably your problem. You need to serialize your json object. And you need to set the .ajax call to type of POST in options otherwise it will default to a get.

wrleskovec
  • 326
  • 1
  • 7
  • What additional information would be useful? I'll try this solution tomorrow, I'm currently busy with another project this evening. – awimley Jan 11 '16 at 22:29
  • 1
    Try changing `res.header('Access-Control-Allow-Origin', "*");` to res.header('Access-Control-Allow-Origin', true); . – wrleskovec Jan 11 '16 at 22:33
  • I have tried both of these approaches and the error message remains unchanged. – awimley Jan 12 '16 at 16:24
  • Well if you ever do figure it out, could you post the solution? – wrleskovec Jan 12 '16 at 17:10
  • Of course, though hopefully I can find some assistance as this has been a persistent problem for me. – awimley Jan 12 '16 at 17:24
  • Wow I looked at it again. I feel so dumb. You need to stringify the testdata object before sending the ajax post. – wrleskovec Jan 12 '16 at 17:27
  • I should have stated in my original post that I had tried raw form data and passing it after running JSON.stringify(testData). Neither resolve the error. – awimley Jan 12 '16 at 18:04
  • did you modify the $.ajax() call to be a post request instead of a get request? – wrleskovec Jan 12 '16 at 19:09