Always do the event error $.ajax
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.Su开发者_开发百科bject = "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 believe you need to add event.preventDefault
to stop the submit event from bubbling up and really submitting the form to the server.
$("div.contato-pedidooracao form").submit(function (event) {
event.preventDefault();
.....
.....
});
Did you try just adding return false to the submit function:
$("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"); }
});
return false; //right here
});
精彩评论