我使用AJAX从PHP文件制作和检索API调用,然后使用jQuery IF语句显示某些数据。
返回的数据带有几种不同的状态,例如:" Ready","正在进行"," DNS"和"错误"。每个状态都有一条消息随附。
对于一个特定的状态,"正在进行",我想继续循环使用JavaScript函数,直到状态变为其他状态。如果状态是"正在进行",它可能需要1分钟才能更改,所以如果可能的话,我想以5秒的间隔循环使用JavaScript功能。
我在下面包含了我的jQuery。
$(document).ready(function() {
//Stops the submit request
$("#myAjaxRequestForm").submit(function(e){
e.preventDefault();
});
//checks for the button click event
$("#myButton").click(function(e){
$("#ajaxResponse").empty();
//get the form data and then serialize that
dataString = $("#myAjaxRequestForm").serialize();
//get the form data using another method
var hostName = $("input#hostName").val();
dataString = "host=" + hostName;
//make the AJAX request, dataType is set to json
//meaning we are expecting JSON data in response from the server
$.ajax({
type: "GET",
url: "api.php",
data: dataString,
dataType: "json",
//if received a response from the server
success: function( response ){
var data = response.status;
if ((data == 'READY' && response.endpoints[0].statusMessage == 'Ready' )) {
$("#ajaxResponse").append("<b>Status:</b> " + response.endpoints[0].statusMessage+ "<br>");
$("#ajaxResponse").append("<b>Host:</b> " + response.host+ "<br>");
$("#ajaxResponse").append("<b>Port:</b> " + response.port+ "<br>");
$("#ajaxResponse").append("<h3>Endpoint [0]</h3><b>Server Name:</b> " + response.endpoints[0].serverName+ "<br>");
$("#ajaxResponse").append("<b>IP Address:</b> " + response.endpoints[0].ipAddress+ "<br>");
$("#ajaxResponse").append("<b>Grade:</b> " + response.endpoints[0].grade+ "<br>");
$("#ajaxResponse").append("<h3>Endpoint [1]</h3><b>Server Name:</b> " + response.endpoints[1].serverName+ "<br>");
$("#ajaxResponse").append("<b>IP Address:</b> " + response.endpoints[1].ipAddress+ "<br>");
$("#ajaxResponse").append("<b>Grade:</b> " + response.endpoints[1].grade+ "<br>");
$("#ajaxResponse").append("<h3>Endpoint [2]</h3><b>Server Name:</b> " + response.endpoints[2].serverName+ "<br>");
$("#ajaxResponse").append("<b>IP Address:</b> " + response.endpoints[2].ipAddress+ "<br>");
$("#ajaxResponse").append("<b>Grade:</b> " + response.endpoints[2].grade+ "<br>");
}
else if ((data == 'READY' && response.endpoints[0].statusMessage != 'Ready')) {
$("#ajaxResponse").append("<b>Status: </b>" +response.endpoints[0].statusMessage+ "<br>");
$("#ajaxResponse").append("<b>Server Name: </b>" + response.endpoints[0].serverName+ "<br>");
$("#ajaxResponse").append("<b>IP Address: </b>" + response.endpoints[0].ipAddress+ "<br>");
}
else if (data == "DNS") {
$("#ajaxResponse").html("<div><b>No cached result found.<br><br>" +response.statusMessage+ "<br>..please wait a few mins and try again.</b></div>");
}
else if (data == "IN_PROGRESS") {
$("#ajaxResponse").html("<div><b>No cached result found.<br><br>" +response.endpoints[0].statusMessage+ "<br>..please wait a few mins and try again.</b></div>");
}
else if (data == "ERROR") {
$("#ajaxResponse").html("<div><b>Error.<br><br>" +response.statusMessage+ "<br></b></div>");
}
else {
$("#ajaxResponse").html("<div><b>error</b></div>");
}
},
});
});
});
答案 0 :(得分:1)
将AJAX请求移动到一个函数(例如doAjax()),以便您可以根据需要调用它。单击按钮时调用doAjax()。当返回的状态为&#34; In Progress&#34;时,使用setTimeout()在5秒后递归调用相同的函数。
AJAX请求大约每5秒发出一次,直到状态不是&#34;进行中&#34;。 (我说&#34;大致&#34;因为AJAX请求本身需要时间,并且在请求完成后5秒延迟开始。)
更新了JavaScript:
$(document).ready(function () {
//Stops the submit request
$("#myAjaxRequestForm").submit(function (e) {
e.preventDefault();
});
//checks for the button click event
$("#myButton").click(function (e) {
e.preventDefault(); // Prevent default button click action
doAjax();
});
});
function doAjax() {
//$("#ajaxResponse").empty(); Move this to reduce appearance of refreshing
//get the form data and then serialize that
dataString = $("#myAjaxRequestForm").serialize();
//get the form data using another method
var hostName = $("input#hostName").val();
dataString = "host=" + hostName;
//make the AJAX request, dataType is set to json
//meaning we are expecting JSON data in response from the server
$.ajax({
type: "GET",
url: "api.php",
data: dataString,
dataType: "json",
//if received a response from the server
success: function (response) {
var data = response.status;
if ((data == 'READY' && response.endpoints[0].statusMessage == 'Ready')) {
$("#ajaxResponse").empty();
$("#ajaxResponse").append("<b>Status:</b> " + response.endpoints[0].statusMessage + "<br>");
$("#ajaxResponse").append("<b>Host:</b> " + response.host + "<br>");
$("#ajaxResponse").append("<b>Port:</b> " + response.port + "<br>");
$("#ajaxResponse").append("<h3>Endpoint [0]</h3><b>Server Name:</b> " + response.endpoints[0].serverName + "<br>");
$("#ajaxResponse").append("<b>IP Address:</b> " + response.endpoints[0].ipAddress + "<br>");
$("#ajaxResponse").append("<b>Grade:</b> " + response.endpoints[0].grade + "<br>");
$("#ajaxResponse").append("<h3>Endpoint [1]</h3><b>Server Name:</b> " + response.endpoints[1].serverName + "<br>");
$("#ajaxResponse").append("<b>IP Address:</b> " + response.endpoints[1].ipAddress + "<br>");
$("#ajaxResponse").append("<b>Grade:</b> " + response.endpoints[1].grade + "<br>");
$("#ajaxResponse").append("<h3>Endpoint [2]</h3><b>Server Name:</b> " + response.endpoints[2].serverName + "<br>");
$("#ajaxResponse").append("<b>IP Address:</b> " + response.endpoints[2].ipAddress + "<br>");
$("#ajaxResponse").append("<b>Grade:</b> " + response.endpoints[2].grade + "<br>");
} else if ((data == 'READY' && response.endpoints[0].statusMessage != 'Ready')) {
$("#ajaxResponse").empty();
$("#ajaxResponse").append("<b>Status: </b>" + response.endpoints[0].statusMessage + "<br>");
$("#ajaxResponse").append("<b>Server Name: </b>" + response.endpoints[0].serverName + "<br>");
$("#ajaxResponse").append("<b>IP Address: </b>" + response.endpoints[0].ipAddress + "<br>");
} else if (data == "DNS") {
$("#ajaxResponse").html("<div><b>No cached result found.<br><br>" + response.statusMessage + "<br>..please wait a few mins and try again.</b></div>");
} else if (data == "IN_PROGRESS") {
$("#ajaxResponse").html("<div><b>No cached result found.<br><br>" + response.endpoints[0].statusMessage + "<br>..please wait a few mins and try again.</b></div>");
// Call this function again after 5 seconds
setTimeout(function () {
doAjax();
}, 5000);
} else if (data == "ERROR") {
$("#ajaxResponse").html("<div><b>Error.<br><br>" + response.statusMessage + "<br></b></div>");
} else {
$("#ajaxResponse").html("<div><b>error</b></div>");
}
},
});
}