weixin_33701617 2015-10-21 13:50 采纳率: 0%
浏览 5

停止AJAX通话

To load my page, I have to make several AJAX POSTS in a particular order and I achieved this by using the .done() function:

$(document).ready(function(){
    // Load Recipients & documents
    $("#page").hide();
    $("#loading").show();

    // Specify the loading order
    load_recipients().done(function(){
        load_documents().done(function() {
            $.when( initialize() ).then(function() {
                var deferreds = [];
                page_list.forEach(function(page_id, index) {
                    deferreds.push(load_fields(page_id));
                });

                $.when.apply(null, deferreds).done(function() {
                    $("#page").show();
                    $("#loading").hide();
                });
            });
        });
    });
});

Now I am trying to implement error handling such that when an error is detected (by me or by HTTP error code) that the loading stops and an error message is displayed, however, I cannot figure out how to do that:

function load_recipients() {
    return $.ajax({
        url: base_url + "recipients/get/",
        type: "POST",
        data: { 
            packets_id : packets_id
        },
        success: function(response_data) {
            recipient_list = JSON.parse(response_data);
            if(recipient_list.hasOwnProperty('error')) {
                 // Make ALL functions stop and throw error message
            } else {
                console.log("Everything is fine");
            }
        },
        error: function(jqXHR){
            // Make ALL functions stop and throw error message
        }
    });
}

So far I've tried e.preventDefault(), .stop(), and stopPropogation() but none of them prevent the next AJAX POST from occurring. Does anyone have any suggestions as to how I can handle this?

  • 写回答

2条回答 默认 最新

  • weixin_33704234 2015-10-21 13:59
    关注

    You could create your own promise that you can resolve or reject yourself.

    function load_recipients() {
        var promise = $.Deferred().promise();
        $.ajax({
            url: base_url + "recipients/get/",
            type: "POST",
            data: { 
                packets_id : packets_id
            },
            success: function(response_data) {
                recipient_list = JSON.parse(response_data);
                if(recipient_list.hasOwnProperty('error')) {
                     promise.reject();
                } else {
                    promise.resolve();
                }
            },
            error: function(jqXHR){
                promise.reject();
            }
        });
        return promise;
    }
    

    As for handling rejections I believe $.when().then handles this.

    https://api.jquery.com/deferred.then/

    deferred.then( doneFilter [, failFilter ] [, progressFilter ] )
    

    doneFilter Type: Function() A function that is called when the Deferred is resolved.

    failFilter Type: Function() An optional function that is called when the Deferred is rejected.

    评论

报告相同问题?

悬赏问题

  • ¥15 outlook无法配置成功
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题