Reputation: 12553
I have a form in my application which I need to submit via Ajax (JQuery), however It needs to get submitted remotely from my JavaScript (i.e. I can not user :remote => true).
I can locate my form in my JavaScript no problems:
my_form = $("#the_id_of_the_form");
Then I create my Ajax request:
$.post('hardcoded path?', function(data) {
// how would I serialize my parameters?
});
Many thanks.
Upvotes: 1
Views: 1454
Reputation: 50057
Do not hardcode the url, instead use the url as stored in the form.
You can do this as follows:
$("#the_id_of_the_form").submit(function() {
$.post({
url: $(this).attr('action'),
data: $(this).serialize()
});
});
Hope this helps.
Upvotes: 1
Reputation: 1124
Try not to use $.post or $.get, it is not agile and cause troubles in debug and refactoring/
$.ajax({
url: 'only_hardcore_path/' + my_form.serialize(),
type: 'POST'
});
or may be:
$.ajax({
url: 'only_hardcore_path/',
type: 'POST',
data: my_form.serialize()
});
Upvotes: 0
Reputation: 2013
from jquery api
Example: send form data using ajax requests
$.post("test.php", $("#testform").serialize());
In your situation it can be something like that.
$('#your_form').submit(function(event) {
event.preventDefault();
$.ajax({
type: "POST",
url: $(this).attr('action'),
data: $(this).serialize(),
dataType: "JSON"
});
});
Upvotes: 2
Reputation: 47472
Use jQuery(<FORM_ID>).ajaxSubmit
Your form should be something like following
<%= form_for @user,:url=>{:controller=>:logins, :action => 'sign_up'}, :html=>{ :onSubmit => "return checkSignupValidation()",:id=>"signup_form"} do |f|%>
<% end %>
And your js
function checkSignupValidation()
{
jQuery('#signup_form').ajaxSubmit({
url:"/logins/sign_up",
iframe: true,
dataType: 'script',
success: function(data){
//console.log(data)
}
});
}
Upvotes: 0