1

this is the piece of the code I have. By default, "ajax_info_box" is hidden, by clicking "button", "ajax_info_box" shows and sends request, it probably takes about 5 seconds to result. How do I cancel and clear the request by click “cancel"? Any help would be appreciated.

   $('.ajax_info_box').hide()
   $('.button').click(function() {
   $('.ajax_info_box').show()
    var xhr =  $.ajax({
        type: "POST",
        url: 
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (data) {
          do something here

    });
    $('.cancel').click(function () {
       suppose to kill the request here
       $('.ajax_info_box').hide()
    }); 
});
7537247
  • 303
  • 5
  • 19

2 Answers2

2

xhr has an abort method on it

xhr.abort()

More here Abort Ajax requests using jQuery

Community
  • 1
  • 1
TGH
  • 38,769
  • 12
  • 102
  • 135
1
   $('.ajax_info_box').hide()
   $('.button').click(function() {
   $('.ajax_info_box').show()
    var xhr =  $.ajax({
        type: "POST",
        url: 
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (data) {
          do something here

    });
    $('.cancel').click(function () {
       xhr.abort();
       $('.ajax_info_box').hide()
    }); 
});