停止JQuery Ajax请求

来源:互联网 发布:支付宝软件 编辑:程序博客网 时间:2024/05/22 05:00
  1. var currentAjax = null;  
  2. function startAjax(){  
  3.     //方法就是将XHR对象指向currentAjax,再调用currentAjax的.abort()来中止请求  
  4.     currentAjax = $.ajax({  
  5.            type:'POST',  
  6.            beforeSend:function(){},  
  7.            url:'test.php',  
  8.            data:'username=xxx',  
  9.            dataType:'JSON',  
  10.            error:function(){alert('error')},  
  11.            success:function(data){alert(data)}  
  12.     });  
  13. }  
  14. function stopAjax(){  
  15.     //如若上一次AJAX请求未完成,则中止请求  
  16.     if(currentAjax) {currentAjax.abort();}  
  17. }  
  18. // --></mce:script>  
  19. </head>  
  20. <body>  
  21. <input type="button" value="触发请求" onclick="startAjax()" />  
  22. <input type="button" value="停止请求" onclick="stopAjax()" />  
  23. </body>  
  24. </html> 
0 0