始终执行事件错误$.ajax
我有一个发送电子邮件的功能。 该功能正常工作,但它总是触发错误功能,没有发生任何错误(我正在收到电子邮件)。
跟随我的Javascript:
//Send mail
$("div.contato-pedidooracao form").submit(function () {
var dataString = $(this).serialize();
$.ajax({
type: "POST",
url: "Contato/SendMail",
data: dataString,
dataType : "json",
success: function (data) { alert("OK"); },
error: function (data) { alert("Error"); }
});
});
控制器
[HttpPost]
public ActionResult SendMail(string name, string phone, string cel, string email, string message)
{
try
{
using (var mail = new MailMessage())
{
mail.To.Add("--mailhere--");
mail.From = new MailAddress("\"" + name + "\" <" + email + ">" );
mail.Subject = "Pedido de Oração - " + name;
mail.Body = message;
mail.IsBodyHtml = false;
new SmtpClient().Send(mail);
}
return Json(new{Sucess = true, Message = "Email enviado com sucess!" } );
}
catch (Exception ex)
{
return Json(new{Sucess = false, Message = ex.Message } );
}
}
I have a function for sending email.
This function works normally, but it always triggers the error function, any error did not occur (I'm receiving email).
Follow my Javascript:
//Send mail
$("div.contato-pedidooracao form").submit(function () {
var dataString = $(this).serialize();
$.ajax({
type: "POST",
url: "Contato/SendMail",
data: dataString,
dataType : "json",
success: function (data) { alert("OK"); },
error: function (data) { alert("Error"); }
});
});
Controller
[HttpPost]
public ActionResult SendMail(string name, string phone, string cel, string email, string message)
{
try
{
using (var mail = new MailMessage())
{
mail.To.Add("--mailhere--");
mail.From = new MailAddress("\"" + name + "\" <" + email + ">" );
mail.Subject = "Pedido de Oração - " + name;
mail.Body = message;
mail.IsBodyHtml = false;
new SmtpClient().Send(mail);
}
return Json(new{Sucess = true, Message = "Email enviado com sucess!" } );
}
catch (Exception ex)
{
return Json(new{Sucess = false, Message = ex.Message } );
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我相信您需要添加
event.preventDefault
来阻止提交事件冒泡并真正将表单提交到服务器。I believe you need to add
event.preventDefault
to stop the submit event from bubbling up and really submitting the form to the server.您是否尝试将 return false 添加到提交函数中:
Did you try just adding return false to the submit function: