0

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?

Nearpoint
  • 7,202
  • 13
  • 46
  • 74
  • see http://stackoverflow.com/questions/21691001/how-do-i-access-http-post-data-from-meteor/21694185#21694185 – Christian Fritz Aug 25 '14 at 03:36
  • Thanks but that is only one half of the puzzle. So I know I have access to post data in the route with the variable this.request.body but now how do I use Iron Router to go to a route and include data in the body? Like how do I make something similar to an ajax request but Meteor style, like with Router.go or something? – Nearpoint Aug 25 '14 at 03:39
  • `$.post`? if you want to do it the meteor way, you'd be using a Meteor method. Have you ruled that option out? – Christian Fritz Aug 25 '14 at 04:54
  • But how does a Meteor method get data to the route? I guess I just have to re-architecture my approach. Basically in my iron router route I find all documents for the user. When the user searches, I want to transfer that search term back to the iron router so it re-renders the route with the query term – Nearpoint Aug 25 '14 at 15:46

1 Answers1

2

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.

Marco de Jongh
  • 5,270
  • 3
  • 17
  • 29
  • Thanks, I knew about Meteor methods but I guess I was dealing with an issue of how to get data into my iron router route. In the route using the data function I send documents back to the page. But if I want to send data to the route and have the data available in the route params so I can render the appropriate documents. – Nearpoint Aug 25 '14 at 15:48
  • I think I understand now what you are looking for. Unfortunately, I don't think that's currently possible, at least not easily. I think you'd like a server-side route that still renders a template based on the POST data. You should look for server-side template packages, for instance there is meteor-handlebars which renders handlebars templates on the server. But you'll have to do that "manually", iron-router template handlers won't help you with that. You can then send the resulting HTML through the `res.send` method. – Christian Fritz Aug 25 '14 at 17:51
  • @nearpoint sounds to me that the solution you have in mind is not compatible with the meteor way of building your application. Maybe you could elaborate on a high level what you're trying to achieve? – Marco de Jongh Aug 25 '14 at 18:32