0

In my below code if input search vale is empty and as well as search keyword is same means if entered 'abc' got the result again clicked need to abort the ajax request, I had written in beforesend method but browser throwing error "Cannot read property 'abort' of undefined"

Ajax code:

    function makeRequest()
    {
    var searchText='';
        var popupRequest = $.ajax({
            url:"cnc/cncstorelocator",
            type:'GET',
            cache:false,
            data: {searchCriteria : $('#cnc-searchcriteria').val()},
            dataType: 'json',
            beforeSend: function(){               
  if(searchText == '' && searchText == searchData) {
                      popupRequest.abort();
                    }
                },
            success : function(cncStoreLocatorData)
            {
                 var store=null;
                for (var i = 0; i < cncStoreLocatorData.length; i++) {
                  var loc = cncStoreLocatorData[i];
                 store = $('<div/>').addClass('pane');
                  var store_hours = loc.hrsOfOperation;
                 var str1 =  $('<p/>').addClass('stores-timing');
                 var store_timings=null;
                  for (var j = 0; j < store_hours.length; j++) {
                         var storetime = store_hours[j];
                        store_timings = str1.append($('<span/>').html('<strong>' + storetime.days_short));
                        store_timings.appendTo(store);
                     }
                  $("#cncstorepane").append(store);
                  searchText=searchData;
                   }
            },
            error: function(cncStoreLocatorData) {
                    alert("can't make req");
                }

          });

    }
5

2 Answers 2

1
    var xhr = $.ajax({
    type: "POST",
    url: "XXX.php",
    data: "name=marry&location=London",
    success: function(msg){
       alert( "The Data Saved: " + msg );
    }
});

//kill the request
xhr.abort()
2
  • As per my code if i abort my ajax request, where popupRequest.abort() added end after the reuest, if I click on search with empty input box, first it showing error alert() and after it is keep on loading.
    – koder
    Commented Jul 11, 2016 at 11:33
  • can you initial it in the constructor etc? Commented Jul 11, 2016 at 11:38
0
var xhr = null;
function makeRequest()
    {
    if( xhr != null ) {
            xhr.abort();
            xhr = null;
        }
    var searchText='';
        xhr = $.ajax({
            url:"cnc/cncstorelocator",
            type:'GET',
            cache:false,
            data: {searchCriteria : $('#cnc-searchcriteria').val()},
            dataType: 'json',
            beforeSend: function(){               
            if(searchText == '' && searchText == searchData) {
                      xhr.abort();
                    }
                },
            success : function(cncStoreLocatorData)
            {
                var store=null;
                for (var i = 0; i < cncStoreLocatorData.length; i++) {
                 var loc = cncStoreLocatorData[i];
                 store = $('<div/>').addClass('pane');
                 var store_hours = loc.hrsOfOperation;
                 var str1 =  $('<p/>').addClass('stores-timing');
                 var store_timings=null;
                 for (var j = 0; j < store_hours.length; j++) {
                    var storetime = store_hours[j];
                 store_timings = str1.append($('<span/>').html('<strong>' + storetime.days_short));
                        store_timings.appendTo(store);
                     }
                  $("#cncstorepane").append(store);
                  searchText=searchData;
                   }
            },
            error: function(cncStoreLocatorData) {
                    alert("can't make req");
                }

          });

Define a variable and give your ajax the same alias. Then, everytime the function is being made, you check if (in this example XHR) is null or not. If it is not, you abort() it and give it null value again.

2
  • Thanks for the quick response, I followed the way you are suggested but it didn't solve my issue, may be am I doing anything wrong, if you see in my code (if(searchText == '' && searchText == searchData) where i need to check whether my input text is null and as well as with same keyword need to abort the ajax request.
    – koder
    Commented Jul 11, 2016 at 12:39
  • 1
    @koder The variable searchData cant even work before this point: if(searchText == '' && searchText == searchData)? At no point you say what searchData is. It seems like you need an ajax call within an ajax call. The first one gets you the searchData and then you can check if the searchData and the searchText are the same, and then you do the Ajax Call you wrote down above. Commented Jul 11, 2016 at 13:14

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