Join the Stack Overflow Community
Stack Overflow is a community of 6.6 million programmers, just like you, helping each other.
Join them; it only takes a minute:
Sign up

"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?

share|improve this question
    
You specify dataType: "json", but your returning html (which needs to be a partial view, not a view) so you need to change it to dataType: "html", (or just omit it). You can also omit contentType: "application/json; charset=utf-8", and just use data: { EmailID: emailAddress }, – Stephen Muecke Oct 9 '15 at 13:00
    
@StephenMuecke : Thanks.. Your suggestion is worked.. – thevan Oct 9 '15 at 13:10
up vote 2 down vote accepted

Note that you are returning a View() from the action. This is effectively html, which is not what jQuery expects and presumably not what you want.

If there is nothing to return, just 200 response for successful execution, return null or empty result:

return new EmptyResult();

Or, if there is some json to return, make sure to call it:

return Json(new {result="Success"});

Also, as a side note, your URL looks a bit weird. Make use of route resolution here:

url: '@Url.Action("ForgotPassword", "Home")'
share|improve this answer
    
I am executing the method. I want to show the success message in div tag. – thevan Oct 9 '15 at 13:04
    
getting compilation error : 'UrlHelper' does not contain a definition for 'ActionLink' – thevan Oct 9 '15 at 13:07
    
@thevan, yes, server-side method is called fine. The problem is that it returns ViewResult, which is html response, while jQuery expects json response (as set in dataType param). You need to decide on one or the other. – Andrei Oct 9 '15 at 13:07
    
@thevan, sorry, that should be just Url.Action, amended – Andrei Oct 9 '15 at 13:08

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.