gpt4 book ai didi

javascript - 如何中止ajax请求

转载 作者:行者123 更新时间:2023-12-03 06:34:10 26 4
gpt4 key购买 nike

在我下面的代码中,如果输入搜索值为空并且搜索关键字相同,则意味着如果输入“abc”再次获得结果,单击需要中止ajax请求,我已经在beforesend方法中编写但浏览器抛出错误“无法读取未定义的属性“abort””

Ajax 代码:

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

});

}

最佳答案

    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()

关于javascript - 如何中止ajax请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38305530/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com