1

I'm trying to solve the problem of session timeouts when doing ajaxy things. I am using jQuery, and have a function set up for when there is an ajax error. If it is a 401 error, I want to basically pause everything, let them log in with a popup window, and then resume everything. I'm not entirely sure, but callbacks and closures seem like words that might help this work, if I knew more about them, and if it is possible to keep track of an ajax request and retry it on some event called later.

So if they are doing the ajax call like this

data = {
    input: value
};
if ($j.statusXHR) {
    $j.statusXHR.abort();
}
$j.statusXHR = $j.ajax({
    url: url,
    data: data,
    type: "POST",
    success: function (data) {
        div.html(data);
    },
    error: function (request, textStatus, error) {
        handleErrors(request, textStatus, error, $j.statusXHR, contentDiv, data);
    }
});

And if they aren't logged in, it goes down into this function, which displays a button to login, which opens a popup window for the login page (can't do this with ajax since openid is on another domain).

function handleErrors(request, textStatus, error, xhr, elem, data) {
    if (request.status === 401) {
        // not logged in
        url = "/login/ajaxExpired";
        if (xhr) {
            xhr.abort();
        }
        $j.ajax({
            url: url,
            success: function (data) {
                elem.html(html);
            }
        });
    }
}

The return_to page of the openid part should detect whether the login was successful, based on the returned parameters, and close the popup window and initiate the original ajax request. This is the part that I'm wondering about. Is it possible?

Radu
  • 8,561
  • 8
  • 55
  • 91
Reese
  • 1,746
  • 1
  • 17
  • 40
  • Seems like what you want, from what I know of the domain, is to have each ajax request be a standalone function, and have a top-level array of each event to be processed, with a flag (remember an array element can be an object) and as you start to process the function, insert into the array, and on completion, check it off in the array. Include a reference to the function that kicked things off in the array, so if you have to login and try again you can just call the function again. I've never tried this, so these are just some pointers. Have everything use a common handleErrors as you suggest – jcolebrand Aug 02 '12 at 22:46
  • possible duplicate of [How do I resend a failed ajax request?](http://stackoverflow.com/questions/8881614/how-do-i-resend-a-failed-ajax-request) – user2284570 Oct 10 '14 at 19:31

0 Answers0