中抛出的异常的jQuery的AJAX回调吞噬? [英] Exceptions thrown in jQuery AJAX callbacks swallowed?
问题描述
有没有办法处理AJAX回调jQuery中抛出的异常,比增try..catch块到每个回调其他?误差函数不叫在这种情况下。
$。阿贾克斯(
{
网址:myurl.rails,
成功:函数(数据)
{
抛出噢,不!;
},
错误:函数(XHR,textStatus,errorThrown)
{
的console.log(AJAX调用失败',XHR,textStatus,errorThrown);
}
});
如果你看一看的jQuery 1.4.2的非精缩版,相关的线是5264至5274. jQuery的只是调用成功
适用于设置函数对象(如果存在的话)没有任何照顾什么样的函数返回或抛出。
你可以做的,而不是为使用 jQuery.ajaxSetup()
像这样:
jQuery.ajaxSetup({
成功:函数(){
尝试 {
如果(this.mysuccess){
this.mysuccess.apply(这一点,参数);
}
}
赶上(错误){
// 随你
}
}
});
和使用 mysuccess
(或任何你会preFER称呼它),而不是成功
在中个人 jQuery.ajax
调用。
Is there any way to handle exceptions thrown from AJAX callbacks in jQuery, other than adding a try..catch block to each callback? The error function is not called in this situation.
$.ajax(
{
url: 'myurl.rails',
success: function( data )
{
throw 'Oh no!';
},
error: function ( xhr, textStatus, errorThrown )
{
console.log( 'AJAX call failed', xhr, textStatus, errorThrown );
}
} );
If you take a look at the non-minified version of jQuery 1.4.2, the relevant lines are 5264 through 5274. jQuery just calls the success
function applied to settings object (if it exists) without any care of what the function returns or throws.
What you can do instead is use jQuery.ajaxSetup()
like so:
jQuery.ajaxSetup({
success: function() {
try {
if ( this.mysuccess ) {
this.mysuccess.apply( this, arguments );
}
}
catch(err) {
// whatever
}
}
});
And use mysuccess
(or whatever you would prefer to call it) instead of success
at the individual jQuery.ajax
calls.
这篇关于中抛出的异常的jQuery的AJAX回调吞噬?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!