Link to home
Avatar of infernothao
infernothao

asked on

IE - Ajax problem - Data necessary to complete this operation is not yet available

Hi Experts, I'm trying to make a call to ajax which it supposes to return the percentage left while waiting for the file to finish generating info. Everything works fine in Firefox but in IE it generates an error as stated in the topic. It does work in IE but with an error. Below is the javascript I used. If I were to comment out the line after  "ajaxRequest.readyState == 3" (line #37)  it seems to work without any error.
/*---------------------------------------------------------

----------------------------------------------------------*/
function generateCompleteExport(data){
	ajaxRequest = GetXmlHttpObject(); 
	ajaxRequest.async = false;
	if (ajaxRequest==null){
		alert ("Browser does not support HTTP Request")
		return
	} 
	
	var loadingImg = '<img src="/reports/assets/scripts/loading.gif" title="Loading...">';
	document.getElementById('status').innerHTML = loadingImg;
	
	// Add parameter 'q' to URL
	var url = 'completeExportGenerate.php?'+data;	
	// Adds a random number to prevent the server from using a cached file
	url=url+"&sid="+Math.random();	
	
	var numTest =0;
	var response;
	var index;
	
	ajaxRequest.onreadystatechange = function(){
		if(ajaxRequest.readyState == 4){
			if(ajaxRequest.status == 200) {
				response = ajaxRequest.responseText.split("###");
				index = response.length-1;
			
				document.getElementById('status').innerHTML =response[index];
			} else {
				//window.status = "No Update for " + targetId;
				document.getElementById('status').innerHTML="Error code " + ajaxRequest.status;
			}
		}
		if(ajaxRequest.readyState == 3){
			response = ajaxRequest.responseText;.split("###");
			if( (response.length-2)<= 0)
				index = 0;
			else
				index = response.length -2;
				
			document.getElementById('status').innerHTML =loadingImg+'<br>'+response[index];
		}
	}
	ajaxRequest.open("GET", url, true);
	ajaxRequest.send(null);
}

Open in new window

Avatar of Gurvinder Pal Singh
Gurvinder Pal Singh
Flag of India image

did you tried removing lines 36-44 and check?
Avatar of apresence
apresence

There is a typo on line 37:
response = ajaxRequest.responseText;.split("###");

It should be:
response = ajaxRequest.responseText.split("###");

About the ready states, you can find more info here:
http://www.devx.com/webdev/Article/33024/0/page/2

State 3 means the page is still loading, so the data is not going to be ready for you yet.
Avatar of infernothao

ASKER

Thanks for answering guys. I'm pretty new to ajax...  anyway, I'm not totally sure what readyState == 3  do but I read some where it says something about interacting between the client and the server. The reason I need line 36-44 is because I want to update the progress bar percent while the file is being generated in the background. Hmm... when I changed the "responseText" to "responseXml" it seems to stop the error. I have tried using responseXml before but I have no clue how to send data back using Php.
@apresence, fixed that typo but still error.
<<I'm not totally sure what readyState == 3  do but I read some where it says something about interacting between the client and the server>>
Not required to handle in most cases.
You can read about it here
http://www.w3schools.com/ajax/ajax_xmlhttprequest_onreadystatechange.asp

In case you want to indicate the user that response is being processed at the background, then you can update those lines to

if(ajaxRequest.readyState == 3){
                  document.getElementById('status').innerHTML ="Response is fetched, loading...";
            }
Well, the thing is it will take anywhere between 5-30 minutes. It would be nice to know how long it will take.
ASKER CERTIFIED SOLUTION
Avatar of apresence
apresence

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Thanks for the advises.
Guess I'm so focus on one think that I couldn't think straight. Thanks for Apresence for not giving up on me and give me something to work on. The solution I came up with is  somewhat a combination of what Apresence's advises. Every 5 seconds or so I'll load and read a XML file generated by PHP. Thanks.