jQuery 之 ajax 的状态函数(15)

jQuery ajaxComplete() 方法

ajaxComplete() 方法规定 AJAX 请求完成时运行的函数。

注意:自 jQuery 版本 1.8 起,该方法只被附加到文档。

不像 ajaxSuccess(),通过 ajaxComplete() 方法规定的函数将在请求完成时运行,即使请求并未成功。

语法

$(document).ajaxComplete(function(event,xhr,options))

===================================================

jQuery ajaxStart() 方法

ajaxStart() 方法规定 AJAX 请求开始时运行的函数。

注意:自 jQuery 版本 1.8 起,该方法只被附加到文档。

语法

$(document).ajaxStart(function())

===================================================

当 AJAX 请求正在进行时显示 "loading" 的指示:

<!DOCTYPE html>
<html>
<head>
<script src="http://libs.baidu.com/jquery/1.10.2/jquery.min.js">
</script>
<script>
$(document).ready(function(){
  $(document).ajaxStart(function(){
    $("#wait").css("display","block");
  });
  $(document).ajaxComplete(function(){
    $("#wait").css("display","none");
  });
  $("button").click(function(){
    $("#txt").load("demo_ajax_load.php");
  });
});
</script>
</head>
<body>
<div id="txt"><h2>使用 AJAX 修改文本</h2></div>
<button>修改内容</button>
<div id="wait" style="display:none;width:69px;height:89px;border:1px solid black;position:absolute;top:50%;left:50%;padding:2px;"><img src='demo_wait.gif' width="64" height="64" /><br>Loading..</div>
</body>
</html>
===================================================

jQuery ajaxStop() 方法

ajaxStop() 方法规定所有的 AJAX 请求完成时运行的函数。

当 AJAX 请求完成时,jQuery 会检查是否存在更多的 AJAX 请求。如果没有其他请求正在等待运行,ajaxStop()方法会运行指定的函数。

注意:自 jQuery 版本 1.8 起,该方法只被附加到文档。

语法

$(document).ajaxStop(function())

function()     

必需。规定所有的 AJAX 请求完成时运行的函数。

===================================================

当所有 AJAX 请求完成时,触发一个提示框:

$(document).ajaxStop(function(){

alert("All AJAX requests completed");

});

===================================================

jQuery ajaxError() 方法

ajaxError() 方法规定 AJAX 请求失败时运行的函数。

注意:自 jQuery 版本 1.8 起,该方法只被附加到文档。

语法

$(document).ajaxError(function(event,xhr,options,exc))
function(event,xhr,options,exc)     

必需。规定当请求失败时运行的函数。

额外的参数:

event - 包含 event 对象

xhr - 包含 XMLHttpRequest 对象

options - 包含 AJAX 请求中使用的选项

exc - 包含 JavaScript exception

===================================================

$(document).ajaxError(function(){

alert("An error occurred!");

});

===================================================

<!DOCTYPE html>
<html>
<head>
<script src="http://libs.baidu.com/jquery/1.10.2/jquery.min.js">
</script>
<script>
$(document).ready(function(){
  $(document).ajaxError(function(e,xhr,opt){
    alert("Error requesting " + opt.url + ": " + xhr.status + " " + xhr.statusText);
  });
  $("button").click(function(){
    $("div").load("wrongfile.txt");
  });
});
</script>
</head>
<body>
<div><h2>Let AJAX change this text</h2></div>
<button>Change Content</button>
</body>
</html>


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值