在$ .ajax成功方法中拒绝jQuery承诺 [英] Rejecting A jQuery Promise In A $.ajax Success Method
本文介绍了在$ .ajax成功方法中拒绝jQuery承诺的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这可能是一个愚蠢的错误,但是这里有。
This is probably a stupid bug, but here goes.
我需要在$ .ajax()调用的成功函数中拒绝jQuery Promise。返回值success是一个布尔值。
I need to reject a jQuery Promise inside the success function of a $.ajax() call. The returned value "success" is a boolean value.
function doSomething() {
var myPromise = $.ajax({
method: "POST",
url: "/url/to/use",
data: {"value":$("#value").val()},
success: function(data) {
if (data.success == false) {
ConfirmMessage.showErrorMessage(data.messages[0]);
return new $.Deferred().reject().promise();
} else {
// do other stuff
}
}
});
return myPromise;
}
doSomething()稍后用于当时( )chain:
The doSomething() is used later on in a then() chain:
doSomething().then(doSomethingElse).then(soOn).then(soForth);
所以我需要能够拒绝承诺并打破链条。
So I need to be able to reject the promise and break the chain.
帮助赞赏。
推荐答案
你不能从成功做到这一点
回调。无论如何都没有理由使用它。只需使用然后
:
You cannot do that from a success
callback. There's no reason to use one anyway. Just use then
:
function doSomething() {
return $.ajax({
method: "POST",
url: "/url/to/use",
data: {"value":$("#value").val()},
}).then(function(data) {
if (data.success == false) {
ConfirmMessage.showErrorMessage(data.messages[0]);
return new $.Deferred().reject().promise();
} else {
// do other stuff
}
});
}
这篇关于在$ .ajax成功方法中拒绝jQuery承诺的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文