d3vkit
d3vkit

Reputation: 1982

Jquery Not Sending Rails The Correct Format From Ajax

Alright, so: Rails 2.1, using jRails and jQuery 1.7.

I have a form I want to submit with ajax; this is inside a fancybox (although I don't think that would make any difference):

<% form_tag({ :action => :mail_by_user }, :id => "order_problem_form") do %>
  ...
  <%= submit_tag "Submit", :class => "downloaded" %>
<% end %>

I have my application.js:

$(document).ready(function () {
  ...
$("#order_problem_form").submit(function() {
  $.ajax({
    type: "POST",
    beforeSend: function(xhr) { xhr.setRequestHeader("Accept", "text/javascript"); },
    dataType: "script",
    url: this.action,
    data: this.serialize()
});

return false;

});

And my controller:

def mail_by_user
  #here we send to delayed_job
  OrderMailer.delay.deliver_problem_with_order(params)
  respond_to do |format|
    format.html
    format.js { render :layout => false }
   end
end

All of this works except that it doesn't send the ajax request as XHR, so format.html is always called. I have added the beforeSend to the request, which is what every post and blog about this problem recommends.

I am not sure what I'm doing wrong - does anyone have any ideas? Best I can think of is jRails is doing something to interfere, but AFAIK it's mainly for hooking up remote_fors and inline js rails stuff. I don't know what it could be doing to my jquery.

Upvotes: 0

Views: 171

Answers (1)

drhenner
drhenner

Reputation: 2230

You can do something like this:

      $("#purchase_cart_order").submit(function(event) {
        // DISABLE THE SUBMIT BUTTON (prevent double clicking)
        $('.downloaded').attr("disabled", "disabled");

        Namespace.order.submitForm();

        return false;
      });

where Namespace.order.submitForm(); is calling your ajax call.

Upvotes: 0

Related Questions