How to make CORS in Meteor.js application working?


#1

I’m trying for few hours to send a POST request from simple index.html page, using Ajax as bellow:

$.ajax({
type: 'POST',
url: 'http://myapp.meteor.com/',
crossDomain: true,
data: {"some":"json"},
dataType: 'json',
success: function(responseData, textStatus, jqXHR) {
    var value = responseData.someKey;
},
error: function (responseData, textStatus, errorThrown) {
    alert('POST failed.');
}
});

Code in my meteorapp.js:

 if (Meteor.isServer) {

  var connectHandler = WebApp.connectHandlers; // get meteor-core's connect-implementation

  Meteor.startup(function () {
    connectHandler.use(function (req, res, next) {
      res.setHeader('Access-Control-Allow-Origin', 'http://origin-domain.org');
      res.setHeader('Access-Control-Allow-Methods', ['GET', 'PUT', 'POST', 'DELETE', 'OPTIONS']);
      res.setHeader('Access-Control-Max-Age', '1000');
      res.setHeader('Access-Control-Allow-Headers', ['Content-Type', 'Authorization', 'X-Requested-With']);
      return next();
    });
  });

http://enable-cors.org/server_meteor.html - this and many others pages say that my solution should work, so please. Can someone review my code and show me what is wrong with it.

EDIT1

If I change my origin domain to ‘origin-domain.org’ than I get following error i the console:

For the ‘origin-domain.org

XMLHttpRequest cannot load http://myapp.meteor.com/myapp.html.
The ‘Access-Control-Allow-Origin’ header contains the invalid value
origin-domain.org’. Origin ‘http://origin-domain.org’ is therefore not
allowed access.

and for the ‘*’ I’m still getting a fail pop-up.

EDIT2

Logged this error:

SyntaxError: Unexpected token <
at Object.parse (native)
at m.parseJSON (https://code.jquery.com/jquery-1.11.3.min.js:5:15998)
at Pb (https://code.jquery.com/jquery-1.11.3.min.js:5:18379)
at x (https://code.jquery.com/jquery-1.11.3.min.js:5:21793)
at XMLHttpRequest.b (https://code.jquery.com/jquery-1.11.3.min.js:5:26030)