欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

js與jQuery終止正在發(fā)送的ajax請(qǐng)求的方法

 更新時(shí)間:2015年12月10日 12:45:58   作者:思齊_  
這篇文章主要介紹了js與jQuery終止正在發(fā)送的ajax請(qǐng)求的方法,實(shí)例分析了jQuery與JavaScript終止ajax請(qǐng)求的實(shí)現(xiàn)技巧與相關(guān)注意事項(xiàng),具有一定參考借鑒價(jià)值,需要的朋友可以參考下

本文實(shí)例講述了js與jQuery終止正在發(fā)送的ajax請(qǐng)求的方法。分享給大家供大家參考,具體如下:

核心:調(diào)用XMLHttpRequest對(duì)象上的abort方法

jquery的ajax方法有自己的超時(shí)時(shí)間設(shè)置參數(shù):

$.ajax({type:'POST',
  url:'b.php',
  data:'',
  timeout:5000,
  success:function(){
  }
})

同時(shí)

1. $.get返回的數(shù)據(jù)類型是XMLHttpRequest,請(qǐng)參考手冊(cè)。($.post、$.ajax、$.getJSON、$.getScript也同樣)

2. XMLHttpRequest對(duì)象有abort()方法

也可以自己手動(dòng)去調(diào)用abort方法:

<script src = "jquery-1.4.4.js"></script>
<script>
var xhr = $.ajax({type:'POST',
  url:'b.php',
  data:'',
  success:function(){
    alert('ok');
  }
})
alert(xhr);
console.log(xhr);
</script>
<button id="song">abort</button>
<script>
$(function(){
  $("#song").click(function(){
    alert('click');
    xhr.abort();
  })
})
</script>

對(duì)于原生的xhr:

xmlHttp.open("POST","theUrl",true);
xmlHttp.onreadystatechange=function(){
  ...//得到響應(yīng)之后的操作
}
xmlHttp.send();
//設(shè)置8秒鐘后檢查xmlHttp對(duì)象所發(fā)送的數(shù)據(jù)是否得到響應(yīng).
setTimeout("CheckRequest()","8000");
function CheckRequest(){
  //為4時(shí)代表請(qǐng)求完成了  
  if(xmlHttp.readyState!=4){
    alert('響應(yīng)超時(shí)');
    //關(guān)閉請(qǐng)求
    xmlHttp.close();
  }
}

希望本文所述對(duì)大家ajax程序設(shè)計(jì)有所幫助。

相關(guān)文章

最新評(píng)論