jQuery Ajax Call返回错误:ASP.NET MVC

时间:2022-12-04 18:22:07

"HomeController.cs" - this is my controller class. "ForgotPassword" - this is the action result method in the above class.

“HomeController.cs” - 这是我的控制器类。 “ForgotPassword” - 这是上述类中的动作结果方法。

    [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:

在SendMail()函数中,我正在向传递的EmailID发送验证链接。我通过jQuery Ajax方法调用此方法,如下所示:

    $('#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".

当按钮点击“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.

一切都很好。调用SendMail()函数,邮件也发送到传递的mailid。但我在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?

我想在视图中显示成功的消息。但是得到错误。究竟是什么问题?怎么解决这个?

1 个解决方案

#1


2  

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.

请注意,您将从操作返回View()。这实际上是html,这不是jQuery所期望的,可能不是你想要的。

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

如果没有要返回的内容,只有200个成功执行的响应,返回null或空结果:

return new EmptyResult();

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

或者,如果有一些json要返回,请务必调用它:

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

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

另外,作为旁注,您的URL看起来有点奇怪。在这里使用路线解决方案:

url: '@Url.Action("ForgotPassword", "Home")'

#1


2  

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.

请注意,您将从操作返回View()。这实际上是html,这不是jQuery所期望的,可能不是你想要的。

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

如果没有要返回的内容,只有200个成功执行的响应,返回null或空结果:

return new EmptyResult();

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

或者,如果有一些json要返回,请务必调用它:

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

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

另外,作为旁注,您的URL看起来有点奇怪。在这里使用路线解决方案:

url: '@Url.Action("ForgotPassword", "Home")'