John
John

Reputation: 13705

How to use jquery to submit a post to a different uri than the current page in Rails

Using the submit function from jquery, how can I get it to post to a uri other than the current page?

For example, I would like the post to go to /comments as opposed to /user/blog. I've looked at the jQuery API page for submit but I don't see something about posting to a different page. Here's the start of the javascript:

jQuery(function() {
  return $('.comment_input').find('form').submit(function(e) {
  });
});

Upvotes: 0

Views: 127

Answers (3)

kitti
kitti

Reputation: 14824

The best way would be to set the action on the form so it doesn't post to the same page:

<form action="/comments">

You can also set it with jQuery on page load:

jQuery(form).attr( 'action', '/comments' );

Upvotes: 1

Justin Helgerson
Justin Helgerson

Reputation: 25531

Why don't you just set the action attribute on the form element?

Upvotes: 0

Senad Meškin
Senad Meškin

Reputation: 13756

  1. Set form action to be url where you want to post

2.Then use jQuery to activate submit button

$('#your-submit-id').click();

But if you want to post it using AJAX, to different domain, then I'm really sorry but that is not possible.

Only one way can be possible is to create invisible iFrame, copy your form into iFrame and then submit it.

Upvotes: 0

Related Questions