中止AJAX请求

  1. <html>  
  2. <head>  
  3. <mce:script type="text/<a href="http://lib.csdn.net/base/javascript" class='replace_word' title="JavaScript知识库" target='_blank' style='color:#df3434; font-weight:bold;'>JavaScript</a>"><!--  
  4. var currentAjax = null;  
  5. function startAjax(){  
  6.     //方法就是将XHR对象指向currentAjax,再调用currentAjax的.abort()来中止请求  
  7.     currentAjax = $.ajax({  
  8.            type:'POST',  
  9.            beforeSend:function(){},  
  10.            url:'test.<a href="http://lib.csdn.net/base/php" class='replace_word' title="PHP知识库" target='_blank' style='color:#df3434; font-weight:bold;'>PHP</a>',  
  11.            data:'username=xxx',  
  12.            dataType:'JSON',  
  13.            error:function(){alert('error')},  
  14.            success:function(data){alert(data)}  
  15.     });  
  16. }  
  17. function stopAjax(){  
  18.     //如若上一次AJAX请求未完成,则中止请求  
  19.     if(currentAjax) {currentAjax.abort();}  
  20. }  
  21. // --></mce:script>  
  22. </head>  
  23. <body>  
  24. <input type="button" value="触发请求" onclick="startAjax()" />  
  25. <input type="button" value="停止请求" onclick="stopAjax()" />  
  26. </body>  
  27. </html>  
原文地址:https://www.cnblogs.com/lx-doit/p/6014995.html