[Ajax在单击按钮时未中止

问题描述 投票:0回答:1

单击按钮后如何中止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();
  }
 }
});

  }, 
)
javascript ajax abort
1个回答
1
投票

您需要一个单独的单击侦听器,该侦听器将终止ajax请求。使用beforeSend并不总是有效。

$('#toStop').click(function(e) {
  e.preventDefault();
  // abort here
  // make sure myajaxreq is accessible in this function
   if (myajaxreq)
   myajaxreq.abort();


 }); 
© www.soinside.com 2019 - 2024. All rights reserved.