简体   繁体   English

如何知道已经达到了ajax的最后响应?

[英]How to know that the ajax last response has been reached?

I am trying to capture/know when it reached the 200(success) for the last and do something ie exit out. 我试图捕获/知道最后一次达到200(成功)的时间,然后执行某些操作,即退出。

How do i know that it has reached the 200 for the last time? 我怎么知道它最后一次达到200?

Here is my code below 这是我的下面的代码

//Main function whihc calls the save and checkstatus function //主函数whihc调用save和checkstatus函数

   $(function () {
        $("#saveChange").click(function () {
            save().complete(function (jqXHR, responseText) {

                var stat = jqXHR.status;
                if (stat == 200) {
                    console.log("inside 200");

                    setTimeout(function () {
                        checkServerStatus().complete(function (jqXHR, responseText) {

                            if (jqXHR.status == 200) {
                                console.log("inside 200");
                                setTimeout(function () {
                                    checkServerStatus().complete(function (jqXHR, responseText) {
                                        count = 0;
                                        count++;
                                        max = 10;
                                        if ((count <= max) && (jqXHR.status == 200)) {
                                            checkServerStatus();
                                            //alert(count);
                                        }

                                    });
                                    if (jqXHR.status == 500) {
                                        // alert(" 500 setimeout inside 500 status "+jqXHR.status + responseText);  
                                    } else if (jqXHR.status == 200) {
                                        // alert(" 200 setimeout inside 200 status "+jqXHR.status + responseText);  
                                    }
                                }, 9000);

                            } else if (jqXHR.status == 500) {
                                // 500 error no submission
                                console.log("inside 500");
                                //alert(" 300 setimeout inside 500 status "+jqXHR.status + responseText);   
                                checkServerStatus();
                            } else {
                                alert("else");
                            }
                        });
                    }, 6000);
                } 

            });


        });
    });

    });

There is a nice discussion about retrying ajax requests here . 有一个关于重试Ajax请求一个很好的讨论在这里 I would advise handling the deferreds yourself. 我建议您自己处理延期付款。 This way you can make a dummy deferred, resolve or reject it at your whim, then chain as usual. 这样,您就可以按照您的想法一时将假人推迟,解决或拒绝,然后照常进行链接。

function checkServerStatus() {
    var dfd = new $.deferred();

    $.ajax({
         url : 'ajaxurl.json',
         tryCount : 0,
         retryLimit : 3,
        success : function(json) {
            dfd.resolve();
        },
        error : function(xhr, textStatus, errorThrown ) {
            if (this.tryCount++ <= this.retryLimit) {
                //try again
               $.ajax(this);
               return;
            }
            dfd.reject();
        }
    });
    return dfd;
}

Chain as usual 照常连锁

checkServerStatus().then(function() {
    // success!
}, function() {
   // reached retry limit :(
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM