"HomeController.cs" - this is my controller class. "ForgotPassword" - this is the action result method in the above class.
[HttpPost]
public ActionResult ForgotPassword(string EmailID)
{
SendMail(EmailID);
return View();
}
In SendMail() function, I am sending a verification link to the passing EmailID. I am calling this method via jQuery Ajax Method like below:
$('#btnSubmit').click(function (e) {
var emailRegex = new RegExp(/^([\w\.\-]+)@@([\w\-]+)((\.(\w){2,3})+)$/i);
//var emailRegex = new RegExp(/^([\w-\.]+)@@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/);
var emailAddress = $("#txtEmail").val();
//alert(emailAddress);
var valid = emailRegex.test(emailAddress);
if (!valid) {
alert("Please Enter Valid Email address");
return false;
} else {
alert(emailAddress);
//return true;
$.ajax({
url: '@Url.Content("~/Home/ForgotPassword")',
async: false,
type: "POST",
data: JSON.stringify({ 'EmailID': emailAddress }),
dataType: "json",
contentType: "application/json; charset=utf-8",
error: function (response) {
alert("Error");
alert(JSON.stringify(response));
//$("#errMsg").css('background', 'red');
//$("#errMsg").html(data.Message);
},
success: function (response) {
//$("#errMsg").css('background', 'green');
//$("#errMsg").html("Mail Sent");
alert(JSON.stringify(response));
}
});
}
});
This is my View:
<input type="text" name="EmailID" id="txtEmail" width="600" />
<input type="submit" name="btnSubmit" id="btnSubmit" value="Send" />
I am calling this method when button click of "btnSubmit".
Everything is working fine. The SendMail() function is called and the mail is also send to the passing mailid. But I am getting error in the jQuery Ajax.
error: function (response) {
alert("Error");
alert(JSON.stringify(response));
//$("#errMsg").css('background', 'red');
//$("#errMsg").html(data.Message);
},
I want to display the successful message in the view. But getting error. What is actually the problem? How to solve this?
dataType: "json",
but your returning html (which needs to be a partial view, not a view) so you need to change it todataType: "html",
(or just omit it). You can also omitcontentType: "application/json; charset=utf-8",
and just usedata: { EmailID: emailAddress },
– Stephen Muecke Oct 9 '15 at 13:00