在成功的ajax重试No .done之后 [英] after successful ajax retry no .done

查看:212
本文介绍了在成功的ajax重试No .done之后的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm making a request to a php file. The response is processed in .done(function(msg){}); and .fail this works fine. But sometimes the request gets an error. I made a retry for this. The retry also works. But if the first time fails and it is successful in de 2 or 3 try my request.done doesn't fire (in firebug I can see that is was successful)

我的请求:

    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并失败:

And this is the .done and fail:

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

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

推荐答案

.done().fail()方法是

The .done() and .fail() methods are part of Deferred Object which is implemented in the jqXHR object returned by $.ajax(). Callbacks which you register with them are not part of $.ajax() options so you can't pass them to another $.ajax(). In your code you are subscribing only to parent $.ajax() Deferred Object callbacks. To achieve the result you want, you should wrap entire operation in another Deferred Object and use .resolveWith()/.rejectWith() methods to pass the proper context. Also you need to remember that Deferred Object can change its state to resolved or rejected only once (in another words if it fails it can't succeed later). So the final code might look like this:

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);
});

这篇关于在成功的ajax重试No .done之后的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆