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.
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.
(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?
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.