Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
366 views
in Technique[技术] by (71.8m points)

javascript - 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,

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You can use XHR's abort.

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

pendingRequest.transport.abort()

would cancel the request.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...