Click here to Skip to main content
11,713,845 members (90,903 online)
Rate this: bad
good
Please Sign up or sign in to vote.
See more: MVC
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.
$(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 29-Apr-13 1:06am

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

<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>
  Permalink  

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

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 330
1 CHill60 315
2 OriginalGriff 165
3 Andy Lanng 138
4 KarstenK 110
0 Sergey Alexandrovich Kryukov 365
1 CHill60 335
2 OriginalGriff 245
3 Andy Lanng 173
4 Richard MacCutchan 150


Advertise | Privacy | Mobile
Web02 | 2.8.150819.1 | Last Updated 29 Apr 2013
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100