[jQuery] Cancel request in jQuery Form plug-in
Similar problems can occur while sending request from search form and
etc., i.e. in the moment when 1st request has not been completed yet,
and user are sending the 2nd request. In case processing of 1st
request takes more time than 2nd one, the similar error occurs.
While investigating jQuery Form plug-in it was discovered that the
plug-in does not contain request identifier and canceling previous
request is impossible.
You should make the following changes in standard jQuery Form plug-in:
1. To create object which contains request identifier
jQuery.extend( jQuery.ajaxSettings, {ajaxTargets: {}} ); in function
$.fn.ajaxSubmit
2. To add request identifier with “target” key
jQuery.ajaxSettings.ajaxTargets[options.target] = $.ajax(options);\ to
this object
3. When a new request is sent, we should check if there is
request with the same target. If yes, stop processing request
try{
if (jQuery.ajaxSettings.ajaxTargets[options.target]) {
jQuery.ajaxSettings.ajaxTargets[options.target].abort();
delete jQuery.ajaxSettings.ajaxTargets[options.target];
}
}catch(e){}
Changes above should be made in jquery.form.js in row 137
$.ajax(options);
Code should be as the following:
if (!jQuery.ajaxSettings.ajaxTargets) {
jQuery.extend( jQuery.ajaxSettings, {ajaxTargets: {}} );
}
try{
if (jQuery.ajaxSettings.ajaxTargets[options.target]) {
jQuery.ajaxSettings.ajaxTargets[options.target].abort();
delete jQuery.ajaxSettings.ajaxTargets[options.target];
}
}catch(e){}
jQuery.ajaxSettings.ajaxTargets[options.target] =
$.ajax(options);
==
Enjoy yourself