Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8531537
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T09:30:27+00:00 2026-06-11T09:30:27+00:00

I’m trying to implement a system of retrying ajax requests that fail for a

  • 0

I’m trying to implement a system of retrying ajax requests that fail for a temporary reason. In my case, it is about retrying requests that failed with a 401 status code because the session has expired, after calling a refresh webservice that revives the session.

The problem is that the “done” callbacks are not called on a successful retry, unlike the “success” ajax option callback that is called. I’ve made up a simple example below:

$.ajaxSetup({statusCode: {
    404: function() {
        this.url = '/existent_url';
        $.ajax(this);
    }
}});

$.ajax({
    url: '/inexistent_url',
    success: function() { alert('success'); }
})
.done(function() {
    alert('done');
});

Is there a way to have done-style callbacks called on a successful retry? I know a deferred can’t be ‘resolved’ after it was ‘rejected’, is it possible to prevent the reject? Or maybe copy the doneList of the original deferred to a new deferred? I’m out of ideas:)

A more realistic example below, where I’m trying to queue up all 401-rejected requests, and retry them after a successful call to /refresh.

var refreshRequest = null,
    waitingRequests = null;

var expiredTokenHandler = function(xhr, textStatus, errorThrown) {

    //only the first rejected request will fire up the /refresh call
    if(!refreshRequest) {
        waitingRequests = $.Deferred();
        refreshRequest = $.ajax({
            url: '/refresh',
            success: function(data) {
                // session refreshed, good
                refreshRequest = null;
                waitingRequests.resolve();
            },
            error: function(data) {
                // session can't be saved
                waitingRequests.reject();
                alert('Your session has expired. Sorry.');
            }
       });
    }

    // put the current request into the waiting queue
    (function(request) {
        waitingRequests.done(function() {
            // retry the request
            $.ajax(request);
        });
    })(this);
}

$.ajaxSetup({statusCode: {
    401: expiredTokenHandler
}});

The mechanism works, the 401-failed requests get fired a second time, the problem is their ‘done’ callbacks do not get called, so the applications stalls.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T09:30:28+00:00Added an answer on June 11, 2026 at 9:30 am

    You could use jQuery.ajaxPrefilter to wrap the jqXHR in another deferred object.

    I made an example on jsFiddle that shows it working, and tried to adapt some of your code to handle the 401 into this version:

    $.ajaxPrefilter(function(opts, originalOpts, jqXHR) {
        // you could pass this option in on a "retry" so that it doesn't
        // get all recursive on you.
        if (opts.refreshRequest) {
            return;
        }
    
        // our own deferred object to handle done/fail callbacks
        var dfd = $.Deferred();
    
        // if the request works, return normally
        jqXHR.done(dfd.resolve);
    
        // if the request fails, do something else
        // yet still resolve
        jqXHR.fail(function() {
            var args = Array.prototype.slice.call(arguments);
            if (jqXHR.status === 401) {
                $.ajax({
                    url: '/refresh',
                    refreshRequest: true,
                    error: function() {
                        // session can't be saved
                        alert('Your session has expired. Sorry.');
                        // reject with the original 401 data
                        dfd.rejectWith(jqXHR, args);
                    },
                    success: function() {
                        // retry with a copied originalOpts with refreshRequest.
                        var newOpts = $.extend({}, originalOpts, {
                            refreshRequest: true
                        });
                        // pass this one on to our deferred pass or fail.
                        $.ajax(newOpts).then(dfd.resolve, dfd.reject);
                    }
                });
    
            } else {
                dfd.rejectWith(jqXHR, args);
            }
        });
    
        // NOW override the jqXHR's promise functions with our deferred
        return dfd.promise(jqXHR);
    });
    

    This works because deferred.promise(object) will actually overwrite all of the “promise methods” on the jqXHR.

    NOTE: To anyone else finding this, if you are attaching callbacks with success: and error: in the ajax options, this snippet will not work the way you expect. It assumes that the only callbacks are the ones attached using the .done(callback) and .fail(callback) methods of the jqXHR.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I'm trying to create an if statement in PHP that prevents a single post
I'm working with an upstream system that sometimes sends me text destined for HTML/XML
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.