Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to you abort/cancel a request using the prototype js library?

Using this code:

var pendingRequest =  new Ajax.Request(myUrl, {
               method: 'post',
               postBody: soapMsg,
               contentType: "text/xml",
               onSuccess: function(transport) {
                    doSomething(transport);
               },
               onFailure: function(t) {
                    OnAjaxFailure(t);
               },
               onException: function(req,exception) { 
                    OnAjaxException(req, exception);          
               } 

            }); 

How can I cancel the request and discard the data, or failing that, how could I identify the request (using a name/guid/whatever) inside my onSuccess method so that I can tell which request is completing?

I was thinking of doing an array.push(pendingRequest) to keep track of pending requests

I want to allow the user to interrupt their request, change the input values and resubmit.

Sometimes the original request ends after the new request and I'm replacing the "correct" data with the "old" data. (search results return 50,000 records on the first query and 5 in the second, for example)

Thanks,

like image 939
Mr. Graves Avatar asked Jul 01 '10 21:07

Mr. Graves


People also ask

How to cancel a request in js?

To be able to cancel fetch , pass the signal property of an AbortController as a fetch option: let controller = new AbortController(); fetch(url, { signal: controller. signal }); The fetch method knows how to work with AbortController .

How do you abort an Ajax call?

Just use ajax. abort(); } //then you make another ajax request $. ajax( //your code here );

Which function is used to cancel the current request in Ajax?

Most of the jQuery Ajax methods return an XMLHttpRequest (or the equivalent) object, so you can just use abort() . See the documentation: abort Method (MSDN). Cancels the current HTTP request.


1 Answers

You can use XHR's abort.

The XHR-object is stored in pendingRequest.transport, so using

pendingRequest.transport.abort()

would cancel the request.

like image 185
Ventero Avatar answered Oct 26 '22 03:10

Ventero