0

We're using jquery-ajaxq to prevent specific requests from running at the same time. It works well except that our client's network sucks. We frequently see timeouts or errors with an HTTP status code of 0 which seems to indicate a connection failure or request cancellation. On those specific occasions when the status code is 0, we'd like to retry x number of times before giving up and displaying a error message to the user.

There are great Stack Overflow answers that show how to do this using $.ajax(this) but those won't work here because we're using ajaxq.

Here's an outline of how our code works now:

doAjax = function(incomingOpts) {
    // do some option defaulting here
    //...

    var options = {
        //...
        success: function(data, textStatus, jqXHR) {
            // basically, if we get here, we could still have an app
            // level error so we check for that and call the appropriate
            // callback
            if ( blah blah ) {
                incomingOpts.handleSuccess(data);
            else
                incomingOpts.handleError(data);
        },
        error: function(jqXHR, textStatus, errorThrown) {
            // throw up a big error dialog to the user with
            // as much data as we can include about what happened
            //...
        }
    };

    _.extend(options, incomingOpts);
    return $.ajaxq(options.url.substring(0, 30), options);
};

It's easy enough in the error method to identify the condition where we'd want to retry in the error function above:

if ( textStatus === "error" && errorThrown === "" && jqXHR.status === "0" ) {
    // then we want to retry
}

What we're having trouble with is how do implement the retry logic.. Also, we'd need to make sure only one call in the stack gets to do the logging of the error if all of them fail and make sure only one calls the appropriate success function if one of the retries actually works.

Community
  • 1
  • 1
Chris Williams
  • 11,647
  • 15
  • 60
  • 97

1 Answers1

0

in my opinion this is a flaw in the ajaxq plugin. It should be setting the context of the callback to the options object or the specified context. I would fix it by modifying line 41 of the plugin to be similar to this (not tested):

var context = options.context || options;
if (originalCompleteCallback) {    
    originalCompleteCallback.call(context, request, status); 
}

Now you can solve it using this in the complete as the original set of options, which you can then reuse to resend the request using the same idea as the answer you linked to.

Kevin B
  • 94,570
  • 16
  • 163
  • 180