.ajaxError()

Description: Register a handler to be called when Ajax requests complete with an error. This is an Ajax Event.
描述: 注册一个当ajax请求失败后将被执行的回调函数。 这是一个ajax事件。起始版本:1.0

.ajaxError( handler(event, jqXHR, ajaxSettings, thrownError) )
handler(event, jqXHR, ajaxSettings, thrownError) 回调函数。
Whenever an Ajax request completes with an error, jQuery triggers the ajaxError event.
无论什么时候,一旦ajax请求失败都会触发ajaxError事件。
Any and all handlers that have been registered with the .ajaxError() method are executed at this time.
所有注册在.ajaxError()中的处理器将在这个时候被执行。

To observe this method in action, set up a basic Ajax load request.
为了观察该方法的实际运行,我们可以建立一个基本的ajax加载请求:

<button class="trigger">Trigger</button>
<div class="result"></div>
<div class="log"></div>
Attach the event handler to any element:
我们可以将事件处理器绑定到任何的元素。

$("div.log").ajaxError(function() {
$(this).text( "Triggered ajaxError handler." );
});
Now, make an Ajax request using any jQuery method:
现在我们可以使用任何一个jQuery Ajax方法建立一个ajax请求:

$("button.trigger").click(function() {
$("div.result").load( "ajax/missing.html" );
});
When the user clicks the button and the Ajax request fails, because the requested file is missing, the log message is displayed.
当用户点击了拥有trigger class的元素并且ajax请求失败(因为找不到指定文件)后log信息将会被显示。

Note: Because .ajaxError() is implemented as a method of jQuery object instances, you can use the this keyword within the callback function to refer to the selected elements.
注意:因为.ajaxError()是作为一个jQuery对象实例方法去执行的,所以我们可以在回调函数中使用this关键字表示当前被选择的元素。

All ajaxError handlers are invoked, regardless of what Ajax request was completed. To differentiate between the requests, you can use the parameters passed to the handler. Each time an ajaxError handler is executed, it is passed the event object, the jqXHR object (prior to jQuery 1.5, the XHR object), and the settings object that was used in the creation of the request. If the request failed because JavaScript raised an exception, the exception object is passed to the handler as a fourth parameter. For example, to restrict the error callback to only handling events dealing with a particular URL:
无论是哪种ajax请求失败时,所有的ajaxError处理器都将会被执行。如果非得区分请求,我们可以根据传递给处理器的参数加于区分。当处理器在执行时,event对象、jqXHR对象(在1.5版本前是XHR对象)、创建该请求的设置对象,这3个对象都会被传递给处理器。如果是因为javascript引起异常导致请求失败时,该异常对象将会被当做第四个参数传递处理器。例如,我们可以限制我们的回调函数只处理特定的URL。

$( "div.log" ).ajaxError(function(e, jqxhr, settings, exception) {
if ( settings.url == "ajax/missing.html" ) {
$(this).text( "Triggered ajaxError handler." );
}
});
Example:
例子:
Show a message when an Ajax request fails.
当ajax请求失败后显示一段信息。
$("#msg").ajaxError(function(event, request, settings){
$(this).append("<li>Error requesting page " + settings.url + "</li>");
});
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值