您的位置:首页 > Web前端 > JQuery

停止jq中的ajax请求用abort()函数

2016-01-07 20:32 676 查看

停止jq中的ajax请求用abort()函数

停止JQ中的ajax请求用abort()函数

<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="触发请求" onclick="startAjax()" />
<input type="button" value="停止请求" onclick="stopAjax()" />
</body>
</html>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息