5

我有一个 jquery-ajax 调用控制器中的一个动作。有时通话失败,所以我想再试一次。

jQuery-ajax:

return ajax({
    url: getRootDir() + urlDoTask, // urlDoTask is something like "/Action/"
    data: { param: someParam },
    type: 'POST',
    dataType: 'json',
    tryCount: 0,
    retryLimit: 3
}).then(function (data) {
    // Do some stuff
    return ....
}, function (xhr, textStatus, errorThrown) {

    if (textStatus == 'timeout' || (xhr.responseText == "" && textStatus == 'error')) {
        this.tryCount++; // this is always 1
        if (this.tryCount <= this.retryLimit) {
            //try again
            $.ajax(this);
            return;
        }
        return; // this point is never reached
    }
    if (xhr.status == 404) {
        // Do some stuff
    }

    return ... // I want to reach it after this.tryCount > this.retryLimit
});

function getRootDir() {
    var loc = window.location.pathname;
    var dir = loc.substring(0, loc.lastIndexOf('/'));

    return dir;
}

上面的代码不起作用,每次 ajax 调用失败时,tryCount 始终为 1。此外,一旦 this.tryCount 大于 this.retryLimit,就永远不会达到最后一次返回。

想法?

4

1 回答 1

1

我看到的东西很少。传入的参数实际上并不是对象的成员。它只是一个密钥/对的 JSON 映射。所以函数中不会存在“this.tryCount”之类的东西。您可以做的是在您的函数(或调试)中放置警报并查看值是什么。可能它们是未定义的或 0。

于 2013-11-03T13:08:15.033 回答