Page 1 of 1

Ajax implementation

Posted: Fri Jun 30, 2006 2:19 am
by rakesh_pd
pickle | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


Hi,
 I m a newbee for AJAX. My purpose is to auto refresh some data from a text file on the web-page. I was working this through php and auto refreshing the page. Now i want to add AJAX to it so that the webpage needn't requires an auto refresh. I copy pasted a baked code to check the feasibilty and got struck. 

The problem i'm facing is that whenever i load the page the requester status <>200,hence the 
rest of the code to parse the data from the text file doesn't work.

Can any one point out where am i making the error? The code is ............

[syntax="javascript"]
<head>
<script language="Javascript">
<!--
var requester;
    function makeRequest() {
    	try 
	{ 
		requester = new XMLHttpRequest(); 
		window.status = "Created XMLHTTP";
	} 
	catch (error) 
	{ 
		try 
		{ 
			requester = new ActiveXObject("Microsoft.XMLHTTP"); 
			window.status = "Created MS XMLHTTP";
		} 
		catch (error) 
		{ 
			return false; 
		} 
	}
      requester.onreadystatechange = alertContents;
     requester.open("POST", "test.txt");        
     requester.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
      requester.send("abc=0");
    }


function alertContents()
{
var lnState = requester.readyState;
	if(lnState == 0)
		window.status = "Sending Request";
	if(lnState == 1)
		window.status = "Loading Response";
	if(lnState == 2)
		window.status = "Response Loaded";
	if(lnState == 3)
		window.status = "Response Ready";		      
	if (lnState == 4) 
	{ 
	     window.status = requester.statusText;
		if (requester.status == 200)		{ 
		 	process_file();  
			window.setTimeout(makeRequest,10000);
		} 
		else 
		{ 
		   document.writeln("bye123")
			no_process_file(); 
			requester.abort();
			window.setTimeout(makeRequest,10000);
		} 
	}
	window.status = requester.readyState;
	return true; 
}

function no_process_file()
{
	alert(requester.status);
	window.status = requester.statusText;
}

function process_file(){
    var freq;
	window.status = requester.statusText;
	var response = requester.responseText;
	freq = response.split(",");
	document.getElementById("dt").innerHTML = freq[0];
	document.getElementById("time").innerHTML = freq[2] ;
    
}
//-->
</script>
</head>

Code: Select all

<body onLoad="makeRequest()">

<table border="1" cellspacing="1" width="100%">
  <tr>
    <td width="50%"><p STYLE="TEXT-ALIGN: center"><font face="Century Gothic" color="blue" size="5"><label id="dt" STYLE="TEXT-ALIGN: center"></label></font></p></td>
    <td width="50%"><p STYLE="TEXT-ALIGN: center"><font face="Century Gothic" color="blue" size="5"><label id="time" STYLE="TEXT-ALIGN: center"></label></font></p></td>
  </tr>
  </table>
</body>
pickle | Please use[/syntax]

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]