使用jQuery,AJAX提交表单,无法获得成功的回调 [英] Submit form with jquery, ajax, can't get success callback
问题描述
好吧,我是pretty的接近这一点,但我需要一点帮助获得渡过了难关。我想通过AJAX,提交表单的一个Rails应用程序,它发送一封电子邮件,然后执行成功的一些code。当我打我的表单中输入的AJAX是工作,因为我得到的电子邮件。然而,成功的code没有运行(也没有为此事的错误)。我也试过完成:但没有工作,所以我怀疑它可能有一些问题,默认形式的行为?想法?
下面是阿贾克斯:
// AJAX功能以无需刷新页面提交电子邮件的形式
$(#contact_me)。递交(函数(){
var数据= $(本).serialize();
$阿贾克斯({
键入:POST,
网址:$(本).attr(行动),
数据:数据集,
成功:函数(){
警报(已发送!);
},
错误:函数(){
警报(出事了!);
}
});
返回false;
})
和铁轨的行动,它呼吁:
高清email_me
名称= PARAMS [:名称]
电子邮件= PARAMS [:邮箱]
内容= PARAMS [:文]
ContactMailer.first_contact(姓名,电子邮件内容).deliver
头:确定
结束
此外,窗体本身
<%=的form_tag({控制器:主,行动:email_me},ID:contact_me,遥控器:真)做%>
< DIV>
< P><%= label_tag(:姓名,姓名)%>
<%= text_field_tag(:名称)%>< / P>
< / DIV>
< DIV>
< P><%= label_tag(:电子邮件,电子邮件)%>
<%= email_field(:用户:电子邮件)%GT;< / P>
< / DIV>
< DIV>
< P><%= label_tag(:文本,什么是您的想法)%>< / P>
<%= text_area_tag(:文本)%>
< / DIV>
<%= submit_tag(说你好!)%>
<%结束%GT;
这是真的那么最终导致的最大问题的小事。我忘了换全功能的函数(){..}。去的身影,现在的伟大工程。感谢您的帮助所有。
$(函数(){
$(#contact_me)。递交(函数(){
var数据= $(本).serialize();
$阿贾克斯({
键入:POST,
网址:$(本).attr(行动),
数据:数据集,
完成:函数(){
警报(已发送!);
},
错误:函数(){
警报(出事了!);
}
});
返回false;
});
})
Alright, I'm pretty close on this, but I need a bit of help getting over the hump. I'm trying to submit a form via AJAX, in a rails app, which sends an email, and then executes some code on success. When I hit enter on my form, the AJAX is working, because I'm getting the email. However, the success code isn't running (nor the error for that matter). I also tried 'complete:' but that didn't work, so I suspect it may be some problem with the default form behavior? Ideas?
Here is the ajax:
// AJAX Function to submit email form without refreshing page
$("#contact_me").submit(function(){
var dataSet = $(this).serialize();
$.ajax({
type: "POST",
url: $(this).attr("action"),
data: dataSet,
success: function(){
alert("Sent!");
},
error: function(){
alert("Something went wrong!");
}
});
return false;
})
And the rails action which it calls:
def email_me
name = params[:name]
email = params[:email]
content = params[:text]
ContactMailer.first_contact(name, email, content).deliver
head :ok
end
Also, the form itself
<%= form_tag({controller: "main", action: "email_me"}, id: "contact_me", remote: "true") do %>
<div>
<p><%= label_tag(:name, "Name") %>
<%= text_field_tag(:name) %></p>
</div>
<div>
<p><%= label_tag(:email, "Email") %>
<%= email_field(:user, :email) %></p>
</div>
<div>
<p><%= label_tag(:text, "What's on your mind?") %></p>
<%= text_area_tag(:text)%>
</div>
<%= submit_tag("Say Hello!")%>
<% end %>
It's really the little things that end up causing the biggest problems. I forgot to wrap the whole function in a function(){..}. Go figure, works great now. Thanks for the help all.
$(function(){
$("#contact_me").submit(function(){
var dataSet = $(this).serialize();
$.ajax({
type: "POST",
url: $(this).attr("action"),
data: dataSet,
complete: function(){
alert("Sent!");
},
error: function(){
alert("Something went wrong!");
}
});
return false;
});
})
这篇关于使用jQuery,AJAX提交表单,无法获得成功的回调的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!