<html>
<head>
<mce:script type="text/javascript"><!--
var currentAjax = null;
function startAjax(){
//方法就是将XHR对象指向currentAjax,再调用currentAjax的.abort()来中止请求
currentAjax = $.ajax({
type:'POST',
beforeSend:function(){},
url:'test.php',
data:'username=xxx',
dataType:'JSON',
error:function(){alert('error')},
success:function(data){alert(data)}
});
}
function stopAjax(){
//如若上一次AJAX请求未完成,则中止请求
if(currentAjax) {currentAjax.abort();}
}
// --></mce:script>
</head>
<body>
<input type="button" value="触发请求" οnclick="startAjax()" />
<input type="button" value="停止请求" οnclick="stopAjax()" />
</body>
</html>