www.webdeveloper.com
Results 1 to 6 of 6

Thread: authentication using post method: handling login failure

Hybrid View

  1. #1
    Join Date
    Mar 2012
    Posts
    4

    authentication using post method: handling login failure

    This may be a really basic question, but I'm having difficulty figuring it out.

    I'm using the JQuery.post method to post user and login data to an API. When the results are good, the "success" function provided to the post method is called. However, when the password is incorrect, nothing happens. I would prefer to notify the user that the password was unsuccessful.

    The comments on the documentation page make reference to a ".ajaxError()" method, and the example refer to a ".error" used as a "promise", but still stuff is getting a bit complex for my taste. I want a simple approach to determine if the form has been processed but the success function has not been called.

    Any help? Is there a canonical approach?

    thanks,
    Dan

  2. #2
    Join Date
    Mar 2012
    Posts
    4
    BTW, here's some code:


    Code:
    function login() {
       $("#emailInput").attr("disabled", true);
       $("#passwordInpout").attr("disabled", true);
       $("#submitButton").attr("disabled", true);
       $.post(
          url . "/authentication/login",
          {
             email:$("#emailInput").val(),
             password:$("#passwordInput").val(),
          },
          processAuthentication,
          "json"
       );
       $("#emailInput").attr("disabled", false);
       $("#passwordInpout").attr("disabled", false);
       $("#submitButton").attr("disabled", false);
    }

  3. #3
    Join Date
    Mar 2012
    Location
    Saint-Petersburg, Russia
    Posts
    97
    Hi!

    It is hard to tell since you do not provide hints on authentication mechanism used. It looks that you send post-request and if it is successful, function "processAuthentication" is called.

    What happens when authentication unsuccessful? You haven't told. You hint that here is some documentation. From what you told about ajaxError it looks that server script sends you error response in case of failed authentication.

    Then you need something like:
    Code:
    $.ajax({
        type: "POST",
        url: "/authentication/login",
        data: {
             email:$("#emailInput").val(),
             password:$("#passwordInput").val(),
        },
        success: processAuthentication,
        error: function() {alert("Hahaha, auth failed!");},
        dataType: "json"
    });
    instead of $.post...

    You may read here about parameters of these functions:
    http://api.jquery.com/jQuery.ajax/
    http://api.jquery.com/jQuery.post/
    Last edited by RodionGork; 03-04-2012 at 12:16 PM.

  4. #4
    Join Date
    Mar 2012
    Posts
    4
    Thanks! I'll try that instead of the .post shortcut.

    The method I'm calling is here.

  5. #5
    Join Date
    Mar 2012
    Location
    Saint-Petersburg, Russia
    Posts
    97
    (you may also use .post without change only adding definition of .ajaxError - though it does not matter)

    I read this link, but it seems bit different. It looks that in case of authentication error your success method is called, but in the received data object you will find a field "error" with description of what had happened.

    Could you simply add an alert to your "processAuthentication" and see, whether it is called when authentication fails?

  6. #6
    Join Date
    Mar 2012
    Posts
    4
    That was my reading of the API doc.... that success should be called but with an error item in the JSON string.

    But indeed your .ajax suggestion worked perfectly. I don't like the idea of defining ajax.error on top of .post: seems like it's violating an abstraction barrier. But doing everything at the .ajax level, as you suggested, seems perfect. Really, the .post call isn't buying much in simplicity anyway. I'd been confused by the original suggestion to do .ajax.error (on the jQuery doc site) until I looked at the source and saw .post is just basically an alias for .ajax anyway.

    So thanks again! Logjam cleared.

    Dan

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles