locked
login RRS feed

  • Question

  • User1519602350 posted

    below is my code jquery/ajax post & cshtml. With the below even if the username and password is wrong it executing the success function but i would like to show an error function. Kindly advise me.

      $.ajax({
                        type: "GET",
                        async: false,
                        url: formAction,
                        data: $formId.serialize(),
                        success: function (data, jqXHR, textStatus) {
     
                            $('.loading').remove();
                            //$formId.fadeOut();
                            $formId.slideDown(function(){
                                setTimeout("$formId.slideUp();",10);                              
                            }); // Show notifications box
                            //$formId.append(data).fadeIn();
                            //$('.success').html(data);
                            //$('.success').fadeIn().show();
                        },
                        error: function (jqXHR, textStatus, errorThrown) {
                            alert(errorThrown);
                        },
                        cache: false, type: 'POST', dataType: 'text'
                    });
    

     if (IsPost) {
            if (WebSecurity.Login(usernames, passwords)) {
                var returnUrl = Request.QueryString["ReturnUrl"];
                if (returnUrl.IsEmpty()) {
                    Response.Redirect(Request.Url.ToString());
                    
                }
                else {
                    Context.RedirectLocal(returnUrl);
                }
            }
            else
                {
                    ModelState.AddFormError("Unable to log in.");
                    @Html.Raw("<br/>unable to login")
                }

    Thanks

    Monday, September 2, 2013 7:31 AM

Answers

  • User-821857111 posted

    The error function is only executed if there is an error in communication with xmlhttprequest. You can pass the result of the function in your response. Then you can determine what actions to take in the success handler based on the content of the response (data).

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, September 2, 2013 3:41 PM
  • User895691971 posted

    Even Facebook, Google and other giants use the form submit to log the users in. Why are you using Ajax to log the user?

    However, as Mike told you the error will be caught only if the ajax doesnot find a page, query is broken, or some other errors. It won't load the errors that are caused by the methods of WebSecurity. 

    But here is a scratch of it, Assume this:

    When you send the ajax form, the ajax gets back with a response. On the login page, you will write the response as this: 

    try {
    // log in the user
     Response.Write("<button>Click to reload the page</button>");
    } 
    catch {
    // the error!
    // get the error type, and write the response..if email password error!
    Response.Write("Email/password not correct! Check again");
    // if server down..
    Response.Write("Its a nice weather outside, go for a walk while we work on server");
    // if other error..
    Response.Write("Wohoo! thanks for telling us about this error!");
    }

    Else what, you can use JSON to get multiple results in Ajax requests. This is the best you can do. However, reloading the page in jQuery success won't be a good idea. As there might be an error too.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, September 6, 2013 12:11 PM

All replies

  • User-821857111 posted

    The error function is only executed if there is an error in communication with xmlhttprequest. You can pass the result of the function in your response. Then you can determine what actions to take in the success handler based on the content of the response (data).

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, September 2, 2013 3:41 PM
  • User895691971 posted

    Even Facebook, Google and other giants use the form submit to log the users in. Why are you using Ajax to log the user?

    However, as Mike told you the error will be caught only if the ajax doesnot find a page, query is broken, or some other errors. It won't load the errors that are caused by the methods of WebSecurity. 

    But here is a scratch of it, Assume this:

    When you send the ajax form, the ajax gets back with a response. On the login page, you will write the response as this: 

    try {
    // log in the user
     Response.Write("<button>Click to reload the page</button>");
    } 
    catch {
    // the error!
    // get the error type, and write the response..if email password error!
    Response.Write("Email/password not correct! Check again");
    // if server down..
    Response.Write("Its a nice weather outside, go for a walk while we work on server");
    // if other error..
    Response.Write("Wohoo! thanks for telling us about this error!");
    }

    Else what, you can use JSON to get multiple results in Ajax requests. This is the best you can do. However, reloading the page in jQuery success won't be a good idea. As there might be an error too.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, September 6, 2013 12:11 PM