Jquery 未从 Ajax 发送正确的格式的 Rails

发布于 2024-12-15 15:42:20 字数 1127 浏览 0 评论 0原文

好吧,所以:Rails 2.1,使用 jRails 和 jQuery 1.7。

我有一个表单想用ajax提交;这是在 fancybox 内(尽管我认为这不会有任何区别):

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

我有我的 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;

});

我的控制器:

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

除了它不将 ajax 请求作为 XHR 发送之外,所有这些都有效,因此始终调用 format.html。我已将 beforeSend 添加到请求中,这是有关此问题的每个帖子和博客都推荐的内容。

我不确定我做错了什么 - 有人有任何想法吗? 我能想到的最好的办法是 jRails 正在做一些干扰,但据我所知,它主要是为了连接 Remote_fors 和内联 js Rails 的东西。我不知道它会对我的 jquery 做什么。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

流星番茄 2024-12-22 15:42:20

你可以这样做:

      $("#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();正在调用您的 ajax 调用。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文