Click here to Skip to main content
15,939,065 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Hi I am trying to create a small login screen. I am not using any API. The way I have set up the login screen is as follow
1. user enter login name and password in VIEW.
2. user clicks login button
3. I validate user details through ajax.
4. At this stage if details are correct, I transfer user to the next page.
5. If details are correct I just put in error message.

Here is the JQuery code I got.
JavaScript
$(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;
                    }

                }
            }
        });
    });
});


As far as my knowledge is concerned that if I return true it should not send call to controller but for some reason it still does. any suggestions how can I stop the call to controller through JQUERY.
Posted

1 solution

JavaScript
<script type="text/javascript">
    $(document).ready(function () {
        var returnType;
        $("#loginButton").click(function (e) {
            e.preventDefault();
            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) {
                            window.location.href = "/Home/Dashboard";
                        }
                        else if (data.loginAuthentication == -2) {
                            $("#password").attr("style", "display:block;");
                            alert("incorrect password");
                            return false;
                        }
                        else if (data.loginAuthentication == -1) {
                            $("#username").attr("style", "display:block;");
                            alert("incorrect un");
                            return false;
                        }

                    }
                }
            });
            //return returnType;
        });
    });
</script>
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900