catching lost connection or lost authentication event using jQuery
I read a couple of articles here that inspired my solution but wondering if i'm doing it the right way.
What I did was to create an event that ca开发者_Python百科tches an jQuery ajax error:
$('body').ajaxError(function(xmlHttpRequest, ajaxOptions, error) {
if (!locked) CheckUserAuthentication();
});
Then the CheckUserAuthentication() goes through another ajax call to a webservice that attempts to check if authentication is still active. if it returns a value that it needs to re-login, I then prompt a modal box that displays a log-in form. If the ajax call fails, then it's safe to assume that internet connection has been lost, so I prompt another modal box that displays a message and a js loop that checks the connection again in intervals.
My first concern is that the event code above actually repeats triggering itself if it reaches to the second ajax error making it a repeating loop. So I made a variable that sets a boolean whether to lock the event to prevent this.
My questions:
1) Am I doing it correctly?
2) If yes, then can I dispose an ajax exception so that i don't need to lock the .ajaxError event from re-triggering?
3) Any other ideas much efficient that what I'm doing?
I appreciate all the help!
I have implemented something like this.
I setup jQuery Ajax options for beforeSend to make an ajax request to see if user is still logged in. If not logged in, I use HTTP Basic Authentication.
HTTP Basic Auth is ugly and lame, but has wonderful modality properties in all browsers in terms of stopping the original Ajax event dead in its tracks while you try to let user log in.
After HTTP Basic Auth, the original Ajax event resumes.
Checkout the javascript source file, which should provide some insights: http://bit.ly/g5jUes
精彩评论