weixin_33739541 2016-07-11 11:13 采纳率: 0%
浏览 101

如何中止ajax请求

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");
                }

          });

    }
  • 写回答

2条回答 默认 最新

  • weixin_33724570 2016-07-11 11:20
    关注
        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()
    
    评论

报告相同问题?