【问题标题】:after successful ajax retry no .done成功ajax重试后没有.done
【发布时间】:2013-07-22 09:15:58
【问题描述】:

我正在向一个 php 文件发出请求。响应在 .done(function(msg){}); 中处理。和 .fail 这工作正常。但有时请求会出错。我为此重试了一次。重试也有效。但是,如果第一次失败并且在 de 2 或 3 中成功,请尝试我的 request.done 不会触发(在 firebug 中我可以看到它是成功的)

我的要求:

    var request = $.ajax({    
                url:            "wcf.php",            
                type:           "POST",     
                dataType:       "xml",
                async:          false,
                timeout:        5000,
                tryCount:       0,
                retryLimit:     3,
                data:      { barcode: value, curPrxID: currentPrxID, timestamp: (new Date).getTime()},
                error: function (xhr, ajaxOptions, thrownError) {
                    if (xhr.status == 500) {
                        alert('Server error');
                    } 
                        this.tryCount++;
                        if (this.tryCount < this.retryLimit) {
                            $.ajax(this);
                            //return;
                        }
                }
           }) ;  

这是 .done 和失败:

request.done(function(msg) 
{
    $(msg).find("Response").each(function()
    {
             // my code here
    });
});

request.fail(function(jqXHR, textStatus, errorThrown) 
{ 
    $("#message").html(errorThrown);    
});

【问题讨论】:

    标签: javascript jquery jquery-deferred


    【解决方案1】:

    .done().fail() 方法是Deferred Object 的一部分,在$.ajax() 返回的jqXHR 对象中实现。您向他们注册的回调不是$.ajax() 选项的一部分,因此您不能将它们传递给另一个$.ajax()。在您的代码中,您只订阅父 $.ajax() Deferred Object 回调。要获得您想要的结果,您应该将整个操作包装在另一个 Deferred Object 中,并使用 .resolveWith()/.rejectWith() 方法传递正确的上下文。您还需要记住,Deferred Object 只能将其状态更改为 resolvedrejected 一次(换句话说,如果它失败了,它就不能以后成功)。所以最终的代码可能是这样的:

    var request = $.Deferred(function(deferred) {
        $.ajax({    
            url: 'wcf.php',
            type: 'POST',
            dataType: 'xml',
            async: false,
            timeout: 5000,
            tryCount: 0,
            retryLimit: 3,
            data: { barcode: value, curPrxID: currentPrxID, timestamp: (new Date).getTime()},
            error: function (xhr, ajaxOptions, thrownError) {
                if (xhr.status == 500) {
                    alert('Server error');
                }
                this.tryCount++;
                if (this.tryCount < this.retryLimit) {
                    $.ajax(this).done(function(data, textStatus, jqXHR) {
                        deferred.resolveWith(this, [data, textStatus, jqXHR]);
                    }).fail(function(jqXHR, textStatus, errorThrown) {
                        if (this.tryCount >= this.retryLimit) {
                            deferred.rejectWith(this, [jqXHR, textStatus, errorThrown]);
                        }
                    });
                }
            }
        }).done(function(data, textStatus, jqXHR) {
            deferred.resolveWith(this, [data, textStatus, jqXHR]);
        });
    }).promise();
    
    request.done(function(msg) {
        $(msg).find("Response").each(function() {
            //Success code here
        });
    });
    
    request.fail(function(jqXHR, textStatus, errorThrown) { 
        $("#message").html(errorThrown);
    });
    

    【讨论】:

    • 谢谢!没想到是这样的。
    猜你喜欢
    • 2012-02-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多