Skip to content Skip to sidebar Skip to footer

Rejecting A JQuery Promise In A $.ajax Success Method

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.

Solution 1:

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
        }
    });
}

Post a Comment for "Rejecting A JQuery Promise In A $.ajax Success Method"