AJAX MVC3 jquery ajax 回调总是在成功时进入

发布于 2024-12-20 07:52:03 字数 975 浏览 2 评论 0原文

我有以下问题,我有一个由 ajax 调用的 post 操作:

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult AX_Login(LoginVM usersubmitted)
{
   if (ModelState.IsValid)
   {
      return Json(new { success = true });
   }
   else
   {
      return Json(new { success=false, error = true });
   }
}

另一方面,我有以下按钮的 jquery 处理程序:

    $("#Jquery_LoginButton").click(
        function () {
         $.ajax({
            type: "POST",
            url: "@(Url.Action("AX_Login","Users"))",
            data:$("#MiniLoginForm").serialize(),
            success: function (result) 
            {
                alert("Good");
            },
            error: function () 
            {
                alert("Bad");
            }
        });

         }
    )

我的问题:已达到操作,但 ajax 调用总是成功结束 - 即使模型无效-。

问题:

  • 如果我设置为 false 成功,您知道为什么会发生这种情况吗?如果模型无效,我可能会抛出异常,但我看不到优雅的东西。

  • 您如何正常管理数据注释和 Ajax 的验证?

im having the following issue i have this post action that is called by ajax:

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult AX_Login(LoginVM usersubmitted)
{
   if (ModelState.IsValid)
   {
      return Json(new { success = true });
   }
   else
   {
      return Json(new { success=false, error = true });
   }
}

For other hand i have the following jquery handler for a button:

    $("#Jquery_LoginButton").click(
        function () {
         $.ajax({
            type: "POST",
            url: "@(Url.Action("AX_Login","Users"))",
            data:$("#MiniLoginForm").serialize(),
            success: function (result) 
            {
                alert("Good");
            },
            error: function () 
            {
                alert("Bad");
            }
        });

         }
    )

My Issue: The action is reached, but always the ajax call ends on sucess - even if the model is not valid -.

Questions:

  • Do you know why this is happening if im setting to false the success? i could throw an exception if the model is not valid but i dont see the thing elegant.

  • How do you manage normally the validation with Data Annotations and Ajax?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

肥爪爪 2024-12-27 07:52:04

你总是会获得“成功”,这是正确的,因为你的要求
总是工作。 $.ajax 不知道返回什么数据。
因此,只有当您的请求失败(例如 404)时,您才会收到错误

您必须执行此操作

$.ajax({
    type: "POST",
    url: "@(Url.Action("AX_Login","Users"))",
    data:$("#MiniLoginForm").serialize(),
    success: function (result) 
    {
        if (result.success) 
        {
            alert("Good");
        } 
        else 
        {
            alert("Bad");
        }
    },
    error: function () 
    {
        alert("Request failed");
    }
});

希望这会有所帮助

It is right that you always get "success" because your request
always work. $.ajax doesnt know what data comes back.
So you will get error only if your request failes (404 for example)

you have to do this

$.ajax({
    type: "POST",
    url: "@(Url.Action("AX_Login","Users"))",
    data:$("#MiniLoginForm").serialize(),
    success: function (result) 
    {
        if (result.success) 
        {
            alert("Good");
        } 
        else 
        {
            alert("Bad");
        }
    },
    error: function () 
    {
        alert("Request failed");
    }
});

hope this helps

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文