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");
    }
});
Augustus
  • 1,479
  • 2
  • 17
  • 31
oops
  • 205
  • 2
  • 11

3 Answers3

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()
Community
  • 1
  • 1
LeNI
  • 1,184
  • 2
  • 10
  • 19
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();
});
A. Wolff
  • 74,033
  • 9
  • 94
  • 155
Manoj Yadav
  • 6,560
  • 1
  • 23
  • 21
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.

Tuhin
  • 3,335
  • 2
  • 16
  • 27