单击按钮后如何中止ajax请求?我尝试了一些在这里找到的答案,但对我无效。
<button type="button" id="toStop">Stop</button>
单击停止按钮时,ajax不会终止。
function() {
var myajaxreq = $.ajax({
url: myurlhere,
type: 'GET',
async: true,
success: function(result) {
}
beforeSend: function () {
if(document.getElementById('toStop').clicked == true) {
myajaxreq.abort();
}
}
});
},
)
您需要一个单独的单击侦听器,该侦听器将终止ajax请求。使用beforeSend并不总是有效。
$('#toStop').click(function(e) {
e.preventDefault();
// abort here
// make sure myajaxreq is accessible in this function
if (myajaxreq)
myajaxreq.abort();
});