15

In phonegap how to cancel an ajax request in program, I would like to set cancel button for control the request when it's too slow

$.ajax({
    type: "GET",
    url: url,
    success: function(m) {
        alert( "success");
    }
});
0

3 Answers 3

9

hi it's similar as Abort Ajax requests using jQuery,anyway This can help you

 var ab = $.ajax({  type: "GET",  url: url,  success: function(m)  {    alert( "success");  } });

//kill the request
ab.abort()
6

Store the promise interface returned from ajax request in a global variable and abort it on cancel click

var result = $.ajax({  type: "GET",  url: url,  success: function(m)  {    alert( "success");  } });


$('#cancel').click(function() {
    result.abort();
});
3
  • can you please elaborate?
    – Tuhin
    Commented Apr 29, 2014 at 11:25
  • You can store the result in global variable and abort the request on a button call Commented Apr 29, 2014 at 11:26
  • 6
    You're not storing the result, you're storing the returned deferred promise, and it exposes the native XMLHttpRequest which has an abort method.
    – adeneo
    Commented Apr 29, 2014 at 11:27
2
var request= $.ajax({  type: "GET",  url: url,  success: function(m)  {    alert( "success");  } });


$('#cancel').click(function() {
    request.abort();
});

this will abort the request from the client(browser) side but note : if the server has already received the request, it may continue processing the request (depending on the platform of the server) even though the browser is no longer listening for a response. There is no reliable way to make the web server stop processing a request that is in progress.

4
  • Wait, how is this different than already posted answer??? EDIT: your words about server not handling it is interresting thought
    – A. Wolff
    Commented Apr 29, 2014 at 11:35
  • I just explained. nothing more than that. OP should know what actually is going to happen not only how it can be achieved.
    – Tuhin
    Commented Apr 29, 2014 at 11:37
  • Ya, edited my previous comment, i'm quite agree here!
    – A. Wolff
    Commented Apr 29, 2014 at 11:37
  • I did not mention that kind of words....:)
    – Tuhin
    Commented Apr 29, 2014 at 11:40

Not the answer you're looking for? Browse other questions tagged or ask your own question.