Click here to Skip to main content
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 2: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 OriginalGriff 505
1 Maciej Los 309
2 BillWoodruff 174
3 /\jmot 160
4 Suraj Sahoo | Coding Passion 160
0 OriginalGriff 8,484
1 Sergey Alexandrovich Kryukov 7,407
2 DamithSL 5,639
3 Maciej Los 5,159
4 Manas Bhardwaj 4,986


Advertise | Privacy | Mobile
Web01 | 2.8.1411023.1 | Last Updated 29 Apr 2013
Copyright © CodeProject, 1999-2014
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