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
Latitude:
= text_field_tag 'latitude_field', '', :size => 10, :class => 'submittable'
Longitude:
= 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() {
$("#get_location_form").submitWithAjax();
});
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
+3
source to share
2 answers
You can see the jquery_ujs script to understand how it does it:
https://github.com/rails/jquery-rails/blob/master/vendor/assets/javascripts/jquery_ujs.js
I think you need to update the RequestHeader like here: https://github.com/rails/jquery-rails/blob/master/vendor/assets/javascripts/jquery_ujs.js#L137
xhr.setRequestHeader('accept', '*/*;q=0.5, ' + settings.accepts.script);
0
source to share