Delete jquery ajax form submit

I remotely submit the form potentially several times in a row. In this particular case, debouncing is not an option. I am looking for the equivalent of jQuery .abort()for remote forms submitted using .submit(), so I can undo all previous views when creating a new one.

Thanks in advance, Garrett

+3
source share
3 answers

What I did was attach to the beforeSend event of the form, save the xhr object as part of the form data and abort it if a new request is in the queue:

$('form').bind("ajax:beforeSend", function(evt, xhr) {
  console.log('Enqueued new xhr request');
  var prevXhr = $(this).data('current-xhr');
  if (prevXhr) {
    prevXhr.abort();
    console.log('Aborting previous xhr request');
  }
  $(this).data('current-xhr', xhr);
});

And you can still use the option :remote => truein the form.

+2
source

, jQuery . queue() .clearQueue(), . ...

+1

I do not understand your problem. If the form is submitted, a request is sent to the server ... so how can you cancel it?

If you cancel, you cancel the response processing, just use the control variable that you increment / decrement to know if there are pending requests.

From the API: http://api.jquery.com/category/ajax/ you cannot do this.

0
source

Source: https://habr.com/ru/post/1794007/


All Articles