开发者

Request periodically coming back empty under race conditions

What might be the possible cause of the following?

I am using an autocomplete script with the following code:

if (that.currentAjaxRequest !== null) {
    that.currentAjaxRequest.abort();
}

that.currentAjaxRequest = $.get(query_string,
    function(data) {
        that.doStuff(data);
    }
);

The script is throttled. So there is a delay of about 300 ms after every keystroke before a request gets sen开发者_开发技巧t.

This works just fine in my test environment. However, I'm running into a situation in the live environment where, periodically, if requests get sent close to eachother (about ever 300 ms), every once in awhile the latest request comes back empty. That is to say, data will be "undefined". Why might this be?


This seems to be a jQuery bug introduced in 1.4: When manually aborting an ajax call, jQuery will call the success function with an empty string instead of the error function.

You can work around this by checking xhr.status in your callback:

function(data, textStatus, xhr) {
    if (xhr.status) {
        //a successful call
    }
    else {
        //an aborted call
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜