return FALSE; $r = well_tag_thread__update(array('id' => $id), $update); return $r; } function well_tag_thread_find($tagid, $page, $pagesize) { $arr = well_tag_thread__find(array('tagid' => $tagid), array('id' => -1), $page, $pagesize); return $arr; } function well_tag_thread_find_by_tid($tid, $page, $pagesize) { $arr = well_tag_thread__find(array('tid' => $tid), array(), $page, $pagesize); return $arr; } ?>javascript - Rejecting A jQuery Promise In A $.ajax Success Method - Stack Overflow
最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Rejecting A jQuery Promise In A $.ajax Success Method - Stack Overflow

programmeradmin3浏览0评论

This is probably a stupid bug, but here goes.

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;

        }

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.

Help appreciated.

This is probably a stupid bug, but here goes.

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;

        }

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.

Help appreciated.

Share Improve this question asked Apr 27, 2016 at 12:11 JasonJason 4,14516 gold badges72 silver badges113 bronze badges 1
  • 1 You can't reject an ajax promise after it's been resolved. Perhaps you can return another promise object which is either marked as resolved or rejected within the ajax callback rather than returning the ajax promise itself. – apokryfos Commented Apr 27, 2016 at 12:19
Add a ment  | 

1 Answer 1

Reset to default 6

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
        }
    });
}
发布评论

评论列表(0)

  1. 暂无评论