1

待機して、200-> 500-> 200から変更されたことを確認するためにajax応答のステータスを確認するにはどうすればよいですか?チェックして再チェックする必要があります。私のコードはここでは正しく機能していません。注:フォームの送信が成功すると、常に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 に答える