how can I cancel an Ajax request?

oop*_*ops 11 javascript ajax jquery cancellation

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");
    }
});
Run Code Online (Sandbox Code Playgroud)

LeN*_*eNI 6

嗨它类似于使用jQuery的Abort Ajax请求,无论如何这可以帮助你

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

//kill the request
ab.abort()
Run Code Online (Sandbox Code Playgroud)


Man*_*dav 5

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();
});
Run Code Online (Sandbox Code Playgroud)

  • 您没有存储结果,而是存储返回的延迟保证,并且它公开了具有中止方法的本机XMLHttpRequest. (5认同)