通过jQuery取消对控制器的调用

时间:2013-04-29 11:10:23

标签: jquery asp.net-mvc

我正在尝试创建一个小型登录屏幕。我没有使用任何API。我设置登录屏幕的方式如下

  1. 用户在VIEW中输入登录名和密码。
  2. 用户点击登录按钮。
  3. 我通过AJAX验证用户详细信息。
  4. 在此阶段,如果细节正确,我会将用户转移到下一页。
  5. 如果细节不正确,我只是输入错误信息。
  6. 这是我得到的JQuery代码。

        $(document).ready(function () {
            var returnType;
            $("#loginButton").click(function () {
                var loginDetails = { 
                    loginName: $("#username").val(),
                    password: $("#password").val()
                };
                $.ajax({
                    url: '@Url.Action("VerifyLoginDetails", "Home")',
                    type: 'POST',
                    contentType: 'application/json',
                    dataType: 'json',
                    data: JSON.stringify(loginDetails),
                    success: function (data) {
                        if (data.success) {
                            if (data.loginAuthentication == 1) {
                                return true;
                            }
    
                            if (data.loginAuthentication == -2) {
                                $("#password").attr("style", "display:block;");
                                alert("incorrect password");
                                return false;
                            }
    
                            if (data.loginAuthentication == -1) {
                                $("#username").attr("style", "display:block;");
                                alert("incorrect un");
                                return false;
                            }
    
                        }
                    }
                });
            });
        });
    

    就我的知识而言,如果我返回true,它不应该将调用发送给控制器,但由于某种原因,它仍然会。关于如何通过jQuery停止对控制器的调用的任何建议?

3 个答案:

答案 0 :(得分:3)

有可能#loginButton处于单击按钮时提交的表单中。您可以使用preventDefault()来阻止此行为:

$("#loginButton").click(function (e) {
  e.preventDefault();
  ...

答案 1 :(得分:0)

就我所见,成功函数的返回值没有任何作用。您应该做的是将重定向代码放在“返回true”的位置。

要执行此操作,请使用window.location.href。

  

window.location.href =“https://google.com”;

答案 2 :(得分:0)

请尝试此代码

  $(document).ready(function () {
        var returnType=false;
        $("#loginButton").click(function () {
            var loginDetails = { 
                loginName: $("#username").val(),
                password: $("#password").val()
            };
            $.ajax({
                url: '@Url.Action("VerifyLoginDetails", "Home")',
                type: 'POST',
                contentType: 'application/json',
                dataType: 'json',
                data: JSON.stringify(loginDetails),
                success: function (data) {
                    if (data.success) {
                        if (data.loginAuthentication == 1) {
                            returnType=true;
                        }

                        if (data.loginAuthentication == -2) {
                            $("#password").attr("style", "display:block;");
                            alert("incorrect password");
                            returnType=false;
                        }

                        if (data.loginAuthentication == -1) {
                            $("#username").attr("style", "display:block;");
                            alert("incorrect un");
                            returnType=false;
                        }

                    }
                }
            });
            return returnType;
        });
    });