views:

76

answers:

1

I'm wondering if anyone has any suggestions as to what the possible cause of the following might be.

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 300ms after every keystroke before a request gets sent.

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 300ms), every once in awhile the latest request comes back empty. That is to say, data will be "undefined". I'm at a bit of a loss as to why this might be.

Any ideas?

Thanks!

+1  A: 

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
    }
}
interjay
Awesome detective work. That was indeed the problem. Thanks.
Travis