Friday, March 30, 2018

What's the best way to retry an AJAX request on failure using jQuery | The best way to retry an AJAX request on failure using jQuery | jQuery AJAX retry

$.ajax({
    url: 'http://',
    type: 'POST',
    data: {},
    tryCount: 0,
    retryLimit: 3,
    success: function (response) {
        //do something
    },
    error: function (xhr, textStatus, errorThrown) {
        this.tryCount++;
        if (this.tryCount <= this.retryLimit) {
            //try again
            $.ajax(this);
            return;
        }
        if (xhr.status == 500) {
            //handle error
        }
        else {
            //handle error
        }
    }
});

No comments:

Post a Comment