Request for periodic return in race conditions
What could be the next reason?
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. Thus, after each keystroke, there is a delay of about 300 μs after the request is sent.
This works very well in my test environment. However, I run into a situation in a live environment where, periodically, if requests are sent close to each other (about 300 μs), every time after a while, the last request is returned empty. That is, the data will be "undefined". Why might this be?
a source to share
This seems to be a jQuery bug introduced in 1.4: when manually interrupting an ajax call, jQuery will call the success function with an empty string instead of the error function.
You can get around this by checking xhr.status
in your callback:
function(data, textStatus, xhr) {
if (xhr.status) {
//a successful call
}
else {
//an aborted call
}
}
a source to share