Skip to main content

How do I resend a failed ajax request?



I have multiple ajax requests some request data every minute others are initiated by the user through a ui.







$.get('/myurl', data).done(function( data ){

// do stuff..

});







The request might fail due to an authentication failure. I've setup a global .ajaxError() method for catching any failed requests.







$(document).ajaxError(function( e, jqxhr ){

// Correct error..

});







After I catch the error I reset authorization. Resetting the authorization works but the user has to manually re initiate the ajax call (through the ui).





How do I resend the failed request using the jqxhr originally sent?





(I'm using jQuery for the ajax)


Comments

  1. In this case, I would write a specific handler for the 403 status code, which means unauthorized (my server would return a 403 too). From the jquery ajax docs, you can do

    $.ajax({
    statusCode: {
    403: function() {
    relogin(onSuccess);
    }
    }
    });


    to achieve that.

    In that handler, I would call a relogin method, passing a function that captures what to do when login succeeds. In this case, you could pass in the method that contains the call you want to run again.

    In the code above, relogin should call the login code, and onSuccess should be a function that wraps the code you execute every minute.

    EDIT- based on your clarification in comment, that this scenario happens for multiple requests, I personally would create an API for your app that captures the interactions with the server.

    app = {};
    app.api = {};
    // now define all your requests AND request callbacks, that way you can reuse them
    app.api.makeRequest1 = function(..){..} // make request 1
    app.api._request1Success = function(...){...}// success handler for request 1
    app.api._request1Fail = function(...){...}// general fail handler for request 1

    /**
    A method that will construct a function that is intended to be executed
    on auth failure.

    @param attempted The method you were trying to execute
    @param args The args you want to pass to the method on retry
    @return function A function that will retry the attempted method
    **/
    app.api.generalAuthFail = function(attempted, args){
    return function(paramsForFail){ // whatever jquery returns on fail should be the args
    if (attempted) attempted(args);
    }
    }


    so with that structure, in your request1 method you would do something like

    $().ajax({
    ....
    statusCode: {
    403: app.api.generalAuthFail(app.api.request1, someArgs);
    }
    }}


    the generalAuthFailure will return a callback that executes the method you pass in.

    ReplyDelete

Post a Comment

Popular posts from this blog

Slow Android emulator

I have a 2.67 GHz Celeron processor, 1.21 GB of RAM on a x86 Windows XP Professional machine. My understanding is that the Android emulator should start fairly quickly on such a machine, but for me it does not. I have followed all instructions in setting up the IDE, SDKs, JDKs and such and have had some success in staring the emulator quickly but is very particulary. How can I, if possible, fix this problem?