Nearpoint
Nearpoint

Reputation: 7362

Meteor: Anyway to use Iron Router methods to call a route with post data?

Using iron router I can currently pass query parameter data to the route by doing something like:

Router.go Router.current().path.split("?")[0] + '?searchTerm=apple'

Which appends a searchTerm to the routes current path. Then in my router file I can access the search term with: this.params.searchTerm

But what if I want to send this data to the route in the body of the request? If I do not want to affect the URL then sending data to the route over the body would be useful. Just like a post ajax request? How can I do that with Router.go or anything else iron router supports?

Basically I want to get data to my route, but I dont want to use session, or affect the url in any way. So my last option is to pass the data in the body, but how?

Upvotes: 0

Views: 1127

Answers (1)

Marco de Jongh
Marco de Jongh

Reputation: 5448

Meteor doesn't work with old school ajax requests.

If you really must accept ajax requests you could (ab)use server-side routes in iron-router:

this.route('serverRoute', {
   where: 'server',
   action: function() {
   this.response.end("THIS IS A SERVER ROUTE..");
}
})

But the accepted meteor way for handling what you described, would be to use Meteor methods on the server side define methods:

Meteor.methods({
  foo: function (arg1, arg2) {
     doStuff(arg1, arg2);
});

Then on the client you call them like so:

Meteor.call('foo', 1, 2, function (error, result) { /* CallbackHandleingCode */ } );

This does not affect the url whatsoever, as internally meteor uses websockets for exchanging data between client and server.

Upvotes: 2

Related Questions