i am making simple autosuggestion (autocompleter) plugin with jQuery. Unfortunately i have to use jsonp. It is ok and it works, but when i am aborting request it will throw
First of all, use some sequencing logic. This will assure your last request prevails over the old ones. Here you have an example.
Also, you can take advantage of these $.ajax settings:
timeout: set a time limit per request and prevent requests fail silently.
beforeSend: validate some conditions before making the call. Complements sequencing helper.
As of jQuery 1.5, the beforeSend option will be called regardless of the type of request.
Example:
function updateResults() {
// Init counter & record current request
if (!window.reqSeq) window.reqSeq = 0;
window.reqSeq +=1;
var thisRequest = window.reqSeq;
$.ajax({
url: [..],
crossDomain: true,
contentType: "application/json",
dataType: 'jsonp',
timeout: 5000, // give 5 seconds of margin
data: { [..] },
beforeSend: function() {
if(thisRequest < window.reqSeq) {
return false;
}
},
success: function(data, textStatus, jqXHR) {
if(thisRequest < window.reqSeq) {
return;
}
else print(data);
},
error: function(jqXHR, textStatus, errorThrown) {
// it'll be called on timeout (error) and beforeSend (abort)
print(textStatus);
}
});
}