Submit remote form_tag in rails 3 using javascript but is html

I have a form_tag in rails 3 as follows and I want to submit this form using ajax instead of html via javascript. When I click the submit button, it is sent as javascript

- form_tag({:controller => "checkin", :action => "main_page"}, :remote => true, :method => :get, :id => "get_location_form" ) do
  = text_field_tag 'latitude_field', '', :size => 10, :class => 'submittable'
  = text_field_tag 'longitude_field', '', :size => 10, :class => 'submittable'
  = submit_tag "Send Info"


Also i am using jQuery so i have these functions in application.js file

$(document).ready(function() {

jQuery.fn.submitWithAjax = function() {
  this.submit(function() {
    $.post(this.action, $(this).serialize(), null, "script");
    return false;
  return this;


Why is it so, when I click the button manually, it makes a javascript request, but when I use form.submit () in javascript to submit the form, it receives it as an HTML request.

Any help is appreciated


source to share

2 answers

You can see the jquery_ujs script to understand how it does it:

I think you need to update the RequestHeader like here:

xhr.setRequestHeader('accept', '*/*;q=0.5, ' + settings.accepts.script);




: just use $("#referenceSubmit").trigger("click");

where referenceSubmit is the ID of the submit button



All Articles