我如何检查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条服务器成功消息。然后重定向页面。所以我试图checkstatus,直到它从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);
}
});
});
});
答案 0 :(得分:4)
试试这个fiddle。
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);