使用jQuery在失败时重试AJAX请求的最佳方法是什么?
伪代码:
$(document).ajaxError(function(e, xhr, options, error) {  xhr.retry()
})
更好的是某种指数补偿
回答:
像这样:
$.ajax({    url : 'someurl',
    type : 'POST',
    data :  ....,   
    tryCount : 0,
    retryLimit : 3,
    success : function(json) {
        //do something
    },
    error : function(xhr, textStatus, errorThrown ) {
        if (textStatus == 'timeout') {
            this.tryCount++;
            if (this.tryCount <= this.retryLimit) {
                //try again
                $.ajax(this);
                return;
            }            
            return;
        }
        if (xhr.status == 500) {
            //handle error
        } else {
            //handle error
        }
    }
});
以上是 使用jQuery在失败时重试AJAX请求的最佳方法是什么? 的全部内容, 来源链接: utcz.com/qa/414157.html

