How to cancel file loading started by ajaxSubmit () in jQuery?

I have code similar to the following:

UploadWidget.prototype.setup_form_handling = function() {
    var _upload_widget = this;
    $('form#uploader')
    .unbind('trigger-submit-form')  // This might be our company own method
    .bind('trigger-submit-form', function() {
        var $form = $(this);
        $form.ajaxSubmit({
            dataType: 'json',
            success: function(data, status, xhr, form) {
                // ...
            },
            error: function(xhr, status, errorThrown) {
                // ...
            }
        });
        return false;
    });
};

Is there a way to use, say, the reset button of a form to cancel the upload process? Or do I need to go to the current page (refresh) to stop everything?

I tried to make a variable saved by the UploadWidget object that saves the jqXHR value (and calls var _upload_widget.jqXHR = $form.ajaxSubmit({ ... });), but I don't think I'm doing it right.

+5
source share
4 answers

Unlike jQuery.ajax(), ajaxForm()and the ajaxSubmit()plugin, jQuery forms do not return a jqXHR object. There are other ways to get the jqXHR object:

data() on the form object (easiest way)

ajaxSubmit() . data() , XHR:

var form = $('#myForm').ajaxSubmit({ ... });
var xhr = form.data('jqxhr');

xhr.abort(), .

​​ 2012 ; . 221. (shukshin.ivan , .)

beforeSend callback ( jQuery Form 2012 )

jQuery, ajaxForm() ajaxSubmit() jQuery.ajax(). beforeSend, beforeSend(jqXHR, settings).

, beforeSend, jqXHR . jqXHR, abort() jqXHR, .

+9

2012 xhr:

var form = $('#myForm').ajaxSubmit({ ... });
var xhr = form.data('jqxhr');
....
$('#cancel').on('click', function(){
    xhr.abort();
});

src - https://github.com/malsup/form/issues/221

+2

You can use this method:

var myForm  = null; 
myForm = $('#myForm').ajaxSubmit({
               url: ajaxUrl,
               beforeSubmit : function() {
                   if(myForm != null)
                      myForm.data('jqxhr').abort();
                   },
               ...
          });

This will stop your previous ajaxSubmit requests.

0
source

Try

window.stop();

which imitates the pressing of the stop button. Not quite sure IE loves it. For IE you can try:

document.execCommand('Stop');
-1
source

All Articles