Jquery 未从 Ajax 发送正确的格式的 Rails
好吧,所以: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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你可以这样做:
where Namespace.order.submitForm();正在调用您的 ajax 调用。
You can do something like this:
where Namespace.order.submitForm(); is calling your ajax call.