Hi,
I am using AJAX to ping a server and check if a file exists. It repeats this operation every second until the file is found.
I can see the file system through a terminal and it appears that the file is being created WELL before the server finds it. I have set the following headers in the PHP script:
header('Pragma: public');
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); // Date in the past
header('Last-Modified: '.gmdate('D, d M Y H:i:s') . ' GMT');
header('Cache-Control: no-store, no-cache, must-revalidate'); // HTTP/1.1
header('Cache-Control: pre-check=0, post-check=0, max-age=0'); // HTTP/1.1
header("Pragma: no-cache");
header("Expires: 0");
I also run clearstatcache() at the top of the file to prevent file_exists from using cached results (according to the PHP website, file_exists does not cache the results when a file does not exist).
My AJAX script prompts the user after about 60 seconds if the file is not found. Once that pops up, if you click "continue", it usually finds the file. I think this is because of the delay. I have tried increasing the delay between AJAX requests...but it does not seem to work.
Any ideas? Here are some snippets from my AJAX script:
var maxPoll = 60;
function PollServerForForm(justPoll,action) {
// Do not let the polling continue for over 60 seconds
maxPoll--;
if(maxPoll <= 0) {
if (confirm("The client has not responded.\n\nContinue waiting?")) {
maxPoll = 60;
} else {
return;
}
}
// Send the request
var d = new Date()
response = SendRequest("script.php?random="+d.getTime());
if (response == "") { return; }
if (response.indexOf("TEMPORARY_FILE_NOT_FOUND") != -1) {
// File not found...search again in 1 second
setTimeout("PollServerForForm(true,'"+action+"');", 1000);
} else {
// do something fun
}
}
// Create string trim() - removes whitespace from beginning and end of a string
String.prototype.trim = function() { return this.replace(/^\s+|\s+$/g, ''); };
function SendRequest(URL, POST_data) {
// Create the XML HTTP Request object
if (typeof(window.ActiveXObject) != "undefined") {
var objRequest = new ActiveXObject("Microsoft.XMLHTTP");
} else {
var objRequest = new XMLHttpRequest();
}
// Open the XML HTTP object
objRequest.open(((typeof(POST_data) != "undefined") ? "POST" : "GET"), URL, false);
// Determine request type
if (typeof(POST_data) == "undefined") {
// Send GET request
objRequest.send(null);
} else {
// Send POST request
objRequest.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
objRequest.send(POST_data);
}
// Return response
response = objRequest.responseText.trim();
// The first line indicates an error
if (response.indexOf("YES\n") == 0) {
document.getElementById("the_status").innerHTML = response.substring(response.indexOf("\n")+1);
document.getElementById("the_status").style.display = "block";
return "";
} else {
return response.substring(response.indexOf("\n")+1);
}
}