jquery Ajax $ .ajaxError [英] jquery Ajax $.ajaxError
问题描述
我有一堆包含成功和错误条件的ajax调用,如下所示:
I have a bunch of ajax calls that contain success and error conditions like this one:
$.ajax({
url: 'Remote/State.cfc'
,type: "POST"
,data: {
'method': 'UpdateStateName'
,'StateID': StateID
,'StateName': StateName
}
,success: function(result){
if (isNaN(result)) {
$('#msg').text(result).addClass('err');
} else {
$('#' + result + ' input[name="StateName"]').addClass('changed');
};
}
,error: function(msg){
$('#msg').text('Connection error').addClass('err');
}
});
所有错误条件都相同。换句话说,他们都在msg id中添加短语连接错误。
All the error conditions are the same. In other words, they all put the phrase "Connection error" in the msg id.
Q1:我可以删除所有这些并用
Q1: Could I remove all these and replace them with
$().ajaxError(function(myEvent, request, settings, thrownError) {
$('#msg').text('Connection error').addClass('err');
});
Q2:您如何使用myEvent并请求显示更详细的错误消息?
Q2: How would you use myEvent and request to display a more informative error message?
推荐答案
Q1。您可以使用 .ajaxError()
这个:
Q1. You can use .ajaxError()
like this:
$(document).ajaxError(function() {
$('#msg').text('Connection error').addClass('err');
});
...或者如果 #msg
元素存在于整个时间,你可以这样做:
...or if the #msg
element is present the entire time, you can do this:
$('#msg').ajaxError(function() {
$(this).text('Connection error').addClass('err');
});
Q2。您可以使用 ajaxError
传入的处理程序参数,它使用以下格式:
handler(event,XMLHttpRequest,ajaxOptions,thrownError)
,如下所示:
Q2. You can use the handler's arguments that ajaxError
passes in, it uses this format:
handler(event, XMLHttpRequest, ajaxOptions, thrownError)
, something like this:
$(document).ajaxError(function(event, request, options, error) {
$('#msg').addClass('err')
.text('Connection error: ' + error + ' when connecting to ' + options.url);
});
注意:在以前版本的jQuery中,这将通过 $。ajaxError()
,因为1.7不再是这种情况,你需要将它附加到你想要它的jQuery对象上,或者 document
如果您不关心特定元素。
Note: in previous versions of jQuery this would be done via $.ajaxError()
, since 1.7 that's no longer the case, you'll need to attach it to a jQuery object you want it on, or document
if you don't care about a particular element.
这篇关于jquery Ajax $ .ajaxError的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!