Reintentar llamado ajax en caso de error

Para hacerlo apropiadamente, me basé exclusivamente en el post de un usuario de stackoverflow aquí.

Definitivamente la mejor solución:

function llamarAjax(callback){
    $.ajax({
        type: 'POST',
        tryCount : 0,
        retryLimit : 3,
        url: 'ImpersonationCheckView.aspx',
        data: {
            'type': 'validateDocuments'
        },
        success: function (result) {
            return callback(result);
        },
        error: function (messageError) {
            this.tryCount++;
            if (this.tryCount <= this.retryLimit) {
                //try again
                $.ajax(this);
                return callback('-1');
            }            
            return callback('-2');
        }
    });
}
llamarAjax(function(retorno){
    alert('haber: ', retorno);
});


Y aquí esta la prueba en vivo.

Espero les sirva.


Sean felices! :) Y siéntanse libres de opinar ;)

No hay comentarios:

Publicar un comentario