Sending data through Restivus into Meteor app usin

2019-05-22 14:29发布

So, here is my code handling post request

$.ajax({
    type: 'POST',
    contentType: 'text/json',
    url: 'http://someapp.meteor.com/api/items/',
    crossDomain: true,
    data: {"name":"John", "age": 34},
    dataType: 'json',
    success: function(responseData, textStatus, jqXHR) {
        var value = responseData.someKey;
        console.log(responseData);
    },
    error: function (responseData, textStatus, errorThrown) {
        alert('POST failed.');
        console.log(responseData);
    }
});

and here is my code inside meteor app:

  // Global API configuration
  var Api = new Restivus({
    enableCors: true,
    apiPath: 'api/'
  });

  // Generates: GET, POST on /api/items and GET, PUT, DELETE on
  // /api/items/:id for the Items collection
  Api.addCollection(SomeCollection);

As a result I get a new entry into my MongoDB collection - but it's empty. There is no data inside it, only this kind of ID id: "ngvHxctZaSmvGojd6". Any ideas what am I missing?

ADDED:

  var connectHandler = WebApp.connectHandlers;
  Meteor.startup(function () {
    connectHandler.use(function (req, res, next) {
      res.setHeader('Access-Control-Allow-Origin', '*');
      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();
    });
  });

SOLVED

I had to delete "contentType" entirely and now it's working fine.

0条回答
登录 后发表回答