can anybody help me with this topic?
I'm doing an ajax post call and I receive status 200 OK but error handler is fired. I don't know if I'm sending wrong data to server or something like that.
Here is the code:
$('#btn_sendMail').on('click', function (ev) {
ev.preventDefault();
var sub = $('#subject').val();
var body = $('#message').val();
var displayNameMailAddress = $('#name').val() + " - " + $('#email').val();
var formData = new FormData();
formData.append('subject', sub);
formData.append('body', body);
formData.append('displayNameMailAddress', displayNameMailAddress);
$.ajax({
type: 'POST',
url: pathRoot() + '/SendMail',
cache: false,
dataType: 'json',
data: formData,
success: function (result) {
if (result.shouldSeeError) {
alertify.error(result.message);
}
else {
alertify.success(result.message);
//$('#emailForm').reset();
}
},
error: function (result) {
alertify.error("Error.");
}
});
});
Server side code (C#):
[HttpPost]
public ActionResult SendMail(string subject, string body, string displayNameMailAddress)
{
string message = string.Empty;
bool error = false;
try
{
Manager.Email.SendEmail(subject, body, displayNameMailAddress);
}
catch (Exception)
{
error = true;
}
message = error != true ? "success" : "error.";
Utils.JsonHelper jsonH = new Utils.JsonHelper()
{
shouldSeeError = error,
message = message
};
if (error == true)
return Json(jsonH, JsonRequestBehavior.DenyGet);
else
return Json(jsonH, JsonRequestBehavior.AllowGet);
}
Can anybody help me?
Thanks.