Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/453.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何定期检查,直到ajax返回状态值发生更改?_Javascript_Jquery_Ajax - Fatal编程技术网

Javascript 如何定期检查,直到ajax返回状态值发生更改?

Javascript 如何定期检查,直到ajax返回状态值发生更改?,javascript,jquery,ajax,Javascript,Jquery,Ajax,如何检查ajax响应的状态以等待并检查它是否已从200-->500-->200更改? 必须反复检查。我的工作不正常这里我的代码如下。 注意:成功提交表单时,它将始终为200。。因此,在再次重定向到主页之前,它需要检查并重新检查500,然后再检查200 我试图创建checkstatus函数,以便可以继续。如何正确地做到这一点 // setTimeout(function() { location.reload(true);}, 3000); function checkS

如何检查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({
类型:“POST”,
url:“foo.json”,
数据:json_数据,
contentType:'应用程序/json',
成功:函数(数据、文本状态、xhr){
log(参数);
控制台日志(xhr.status);
警报(“您的更改正在提交:“+textStatus+”:“+xhr.status”);
$('#myModal').modal('hide');
$('#myModal loading').modal('show');
//调用函数checkstatus当前不工作
//statsu val==200服务器初始表单来自服务器的成功val
setTimeout(函数(){
计数=0;
var statusval=checkStatus();
而(statusval==200,count试试看

HTML:

代码:

函数waitForStatus(url、状态、maxRetries、成功、失败){
var计数=0;
var restartCheck=setInterval(函数(){
计数++;
如果(计数尝试一下

HTML:

代码:

函数waitForStatus(url、状态、maxRetries、成功、失败){
var计数=0;
var restartCheck=setInterval(函数(){
计数++;

如果(计数如果它有一个500错误,它将永远不会进入成功功能,对吗?对于成功表单提交,服务器将始终给出一个200消息。因此,我们必须等待重新检查,直到我们从服务器收到500个错误消息,它将使用statuscheck重新启动。我们必须等待使用statuscheck重新检查,以查看它在服务后是否已更改为200。)在我重新返回主页之前,请先阅读错误日志。啊,好吧,我想我现在就要开始了。我会在几秒钟后为您发布一个代码片段。花点时间运行代码是值得的(),您的代码中有很多错误。如果它有500个错误,它将永远不会进入成功功能,对吗?对于成功表单提交,服务器将始终给出200条消息。因此,我们必须等待重新检查,直到从服务器收到500条错误消息,表明它正在使用statuscheck重新启动。我们必须使用statuscheck等待重新检查以进行检查ee在服务器重新启动后,在我重新返回主页之前,它已更改为200。啊,好的,我想我现在就开始。我将在几秒钟后为您发布一个片段。花点时间运行代码是值得的(),代码中有很多错误。谢谢。快速提问,您是如何获得“/echo/xml”和“/echo/xml/404”的我将只传递URL链接到json文件foo.json。2如果我只调用函数(status)或函数wll,它们将返回值“successCallback”和“failureCallback”,或者我需要做更多的事情来获取这些值?显然,我不能使用您的链接,因为我没有访问您的服务器的权限-“/echo/xml”和/echo/xml/404“正好适用于JSFIDLE.net。至于后者,你应该真正研究回调和异步代码的工作原理,现在不是解释的时候或地点。我建议
JavaScript:the Good Parts
作者
Douglas Crockford
谢谢。快速提问,你是如何获得“/echo/xml”和“/echo/xml/404”的?”我将只传递URL链接到json文件foo.json。2如果我只调用函数(status)或函数wll,它们将返回值“successCallback”和“failureCallback”,或者我需要做更多的事情来获取这些值?显然,我不能使用您的链接,因为我没有访问您的服务器的权限-“/echo/xml”和/echo/xml/404“恰好适用于JSFIDLE.net。至于后者,您应该真正研究回调和异步代码的工作原理,现在不是解释这一点的时间或地点。我推荐道格拉斯·克罗克福德的
JavaScript:the Good Parts
    $.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);

        }
    });

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