【发布时间】:2014-02-02 08:22:40
【问题描述】:
一个说明我想做什么的例子。这是我通常会做的:
function success(data, status, jqxhr){
if ( data.error )
return failure(jqxhr, status, data.error);
// process data
}
function failure(jqxhr, status, err){
...
}
$.ajax( ... )
.done(success)
.fail(failure)
有什么办法,我可以像这样只用匿名函数来完成吗?
$.ajax( ... )
.done(function(data, status, jqxhr){
if(data.error)
// what do i need to do here to jump in to the fail handler?
})
.fail(function(jqxhr, status, err){
...
})
【问题讨论】:
-
延迟对象的状态只能设置一次......
-
我知道。还有什么方法可以强制执行失败处理程序吗?
-
从技术上讲,您可以使用 goto。但实际上最好不要这样做
-
用
.always()怎么样? -
@NabilKadimi,那么我需要额外的检查,无论是否总是在成功或失败后被调用,但这是一个想法
标签: javascript jquery jquery-deferred