Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

HTTP request not working properly 1

Status
Not open for further replies.

chrismassey

Programmer
Aug 24, 2007
264
GB
Hey,

I am using a javascript/AJAX HTTP Request to automatically refresh a perl script which simply generates a random number. The plan is to reload the perl script every 5 seconds therefore displaying a new random number each time. However, a javascript error is produced, although the perl script runs and is printed, it never refreshes.

Im guessing that there is something wrong with the fRefresh function, and any help or suggestions as to what I am doing wrong will be much appreciated...

Code:
<html>
<head>
<script type="text/javascript">

window.onload = fInitial;
var xhr = null;

function fInitial() {

	if (window.XMLHttpRequest) {
		xhr = new XMLHttpRequest();	
	}
	else {
		if (window.ActiveXObject) {
			try {
				xhr = new ActiveXObject("Microsoft.XMLHttp");
			}
			catch (e) {}
		}
	}
	if (xhr) {
		fRefresh();
	}
	else {
		alert("XML Http Request Could Not Be Processed.");
	}

}

function fRefresh() {

	var url="rng.pl";
	xhr.open("GET",url,false);
	xhr.onreadystatechange = fDisplay;
	xhr.send(null);
	setTimeout("fRefresh",5 * 1000);

}

function fDisplay() {
	document.getElementById("DisplayNum").innerHTML=xhr.responseText;
}

</script>
</head>
<body>
<span id="DisplayNum"></span>
</body>
</html>

Thanks,

Chris
 
Thanks for responding,

I recieve the same problem. I ran my script through a javascript syntax checker and I recieved a number of errors

Code:
Error:
Implied global: ActiveXObject 12, XMLHttpRequest 7, alert 21, document 37, setTimeout 32, window 1 6





Problem at line 4 character 18: 'fInitial' was used before it was defined.

function fInitial() {

Problem at line 26 character 18: 'fRefresh' was used before it was defined.

function fRefresh() {

Problem at line 32 character 5: Implied eval is evil. Pass a function instead of a string.

setTimeout("fRefresh();",5 * 1000);

Problem at line 36 character 18: 'fDisplay' was used before it was defined.

function fDisplay() {

I'm new to this kind of javascript programming and I copied most of the code from a book. I hope to use a similar script to create a ajax/perl chatroom and i'm really eager to get this working. Thanks for all your help
 
[1] Preliminary (see final form below)
>xhr.open("GET",url,false);
[tt]xhr.open("GET",url,[red]true[/red]);[/tt]

[2]
[tt]function fDisplay() {
[red]if (xhr.readyState==4) {[/red]
document.getElementById("DisplayNum").innerHTML=xhr.responseText;
[red]}[/red]
}
[/tt]
[3]
>setTimeout("fRefresh",5 * 1000);
[tt]setTimeout("fRefresh[red]()[/red]",5 * 1000);[/tt]

[1'] Final form

[tt]xhr.open("GET",url[blue]+"?qazxsw="+(new Date()).getTime()[/blue],[red]true[/red]);[/tt]

where qazxsw is some exotic enough dummy querystring variable, and the getTime() is equally arbitrary setup as long as it is random enough to be different each time xhr is open representing a new call.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top