1

我如何检查 ajax 响应的状态以等待并检查它是否已从 200 --> 500 --> 200 更改?它必须检查并重新检查。我的在下面的代码中无法正常工作。注意:在成功提交表单时,它将始终为 200 .. 因此它需要检查并重新检查 500,然后再检查 200,然后再次重定向到主页。

我试图创建 checkstatus 函数,以便我可以重用。如何正确地做到这一点?

        // setTimeout(function() { location.reload(true);}, 3000);
     function checkStatus() {
            /***  Re check bit ***/
            var restartCheck = window.setInterval(
            $.ajax({
                // dataType : 'jsonp',
                //jsonp : 'js',
                url: "../../../../../rest/configuration",
                beforeSend: function (jqXHR, settings) {
                    console.info('in beforeSend');
                    console.log(jqXHR, settings);
                },
                error: function (jqXHR, textStatus, errorThrown) {
                    alert(" 500 top data still loading " + jqXHR + " : " + textStatus + " : " + errorThrown);
                    console.info('in error');
                    console.log(jqXHR, textStatus, errorThrown);

                },
                complete: function (jqXHR, textStatus) {
                    alert(" complete " + jqXHR + " : " + textStatus);
                    console.info('in complete');
                    console.log(jqXHR, textStatus);
                },
                success: function (data, textStatus, jqXHR) {
                    window.clearInterval(restartCheck);
                    alert(" success " + jqXHR + " : " + textStatus);
                    console.info('in success');
                    console.log(data, textStatus, jqXHR);
                }
            }), 3000); //This will call the ajax function every 3 seconds until the clearInterval function is called in the success callback.
            /*** recheck bit **/

}

/** 在初始成功表单提交时,成功值为 200。之后我有时需要等待检查并重新检查 500 是否服务器启动,当服务器重新启动或重新启动时,它会给出 500 内部服务器消息,大约需要 30到 40 秒或更长时间来重新启动服务器,所以我有 30 秒的等待时间。一旦服务器重新启动,它会给出 200 成功服务器消息。然后重定向页面。所以我正在尝试检查状态,直到它从 200 --> 500 -200 **/

    $.ajax({
        type: "POST",
        url: "foo.json",
        data: json_data,
        contentType: 'application/json',
        success: function (data, textStatus, xhr) {
            console.log(arguments);
            console.log(xhr.status);
            alert("Your changes are being submitted: " + textStatus + " : " + xhr.status);
            $('#myModal').modal('hide');
            $('#myModal-loading').modal('show');

            //call function checkstatus  not currently workign
            //statsu val ==200 server inital form success val from server
            setTimeout(function () {
              count=0;
             var statusval = checkStatus();
            while(statusval == 200 and count <=5) {
               statusval = checkStatus();
 //statsu val has changed to 500 server is restarting

               if (statusval==500) {
               //wait for   30 sec to recheck if the server has restarted and changed to success ie 200 
                setTimeout(function () { checkStatus();}, 30000);
               }

             count++;
            }, 3000);

            alert("restartCheck " + restartCheck)
            setTimeout(function () {
                location.reload(true);
            }, 3000);
            // $('#myModal-loading').modal('hide');
            $('<div id="loading">Loading...</div>').insertBefore('#myform');
            //location.reload(true);
        },
        error: function (jqXHR, textStatus, errorThrown) {
            // alert("Warning and error has occured: "+errorThrown + " : " + jqXHR.status);
            alert(jqXHR.responseText + " - " + errorThrown + " : " + jqXHR.status);

        }
    });

    });
    });
4

1 回答 1

4

试试这个小提琴。

HTML:

<div></div>​

代码:

function waitForStatus(url, status, maxRetries, success, failure) {
    var count = 0;
    var restartCheck = setInterval(function() {
        count++;
        if (count <= maxRetries) {
            $.ajax({
                url: url,
                type: "POST",
                complete: function (jqXHR, textStatus) {
                    if (jqXHR.status == status) {
                        clearInterval(restartCheck);
                        success(status);
                    }
                }
            });
        } else {
            clearInterval(restartCheck);
            failure();
        }
    }, 3000);
}

var successCallback = function(status) { 
    $("div").append('<p>SUCCESS: returned ' + status + '</p>');
}; 

var failureCallback = function() {
    $("div").append('<p>FAILURE: max attempts reached</p>');
};

// This will succeed    
waitForStatus('/echo/xml', 200, 5, successCallback, failureCallback);

// This will fail
waitForStatus('/echo/xml/404', 200, 5, successCallback, failureCallback);
于 2012-12-03T16:30:32.340 回答