Web Design Forum: Ajax readystate problem - Web Design Forum

Jump to content

WDF
WDF Premium Memberships Reseller Hosting
Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Ajax readystate problem my readystate is always returning 0 Rate Topic: -----

#1 User is offline   hire202 

  • Forum Newcomer
  • Pip
  • Group: Members
  • Posts: 14
  • Joined: 03-May 09
  • Reputation: 0
  • Gender:Male
  • Location:United Kingdom
  • Experience:Advanced
  • Area of Expertise:Web Designer

Posted 02 August 2009 - 03:48 PM

Hello, of all the coding I've done, AJAX has been something that i've never used before because PHP seems to be my first resort.So being new to AJAX I started at the basic level, I wanted for a test to be able to present the readystate change into a loading image, so basically when ready state is 0,1,2,3 it will show a loading image and when the ready state is 4 it won't show the image.

I made a basis script that consists of this code:
function getXmlHttpObject() {
	if (window.XMLHttpRequest) {
	  return new XMLHttpRequest();
	}
	if (window.ActiveXObject) {
	  return new ActiveXObject("Microsoft.XMLHTTP");
	}
	return null;
}

function loading(connection) {
	var timer = setTimeout(function() { loading(connection); }, 1);
	var objLoadingImg = document.getElementById('loading-image');
	if(connection.readyState === 0) {
		objLoadingImg.style.visibility = 'visible';
	}
	else if(connection.readyState == 1) {
		objLoadingImg.style.visibility = 'visible';
	}
	else if(connection.readyState == 2) {
		objLoadingImg.style.visibility = 'visible';
	}
	else if(connection.readyState == 3) {
		objLoadingImg.style.visibility = 'visible';
	}
	else if(connection.readyState == 4) {
		objLoadingImg.style.visibility = 'hidden';
		clearTimeout(timer);
	}
}

function loadConnection() {
	var response = getXmlHttpObject();
	response.onreadystatechange = loading(response);
}


Here is a link to the script in action: Script Example

The problem is the readystate is always returning 0 and nothing else, I don't think it's a syntax error, probably a logic error, pleae help me.
0

#2 User is offline   swg 

  • Forum Newcomer
  • Pip
  • Group: Members
  • Posts: 3
  • Joined: 02-August 09
  • Reputation: 0

Posted 02 August 2009 - 07:01 PM

I think it's because you're not asking it to do anything.

You've not sent any Ajax request so the state is always 0, i.e. "the request is not initialized"
0

#3 User is offline   hire202 

  • Forum Newcomer
  • Pip
  • Group: Members
  • Posts: 14
  • Joined: 03-May 09
  • Reputation: 0
  • Gender:Male
  • Location:United Kingdom
  • Experience:Advanced
  • Area of Expertise:Web Designer

Posted 02 August 2009 - 07:06 PM

Oh I see, I will try this, thankyou very much :-).

**EDIT**

Yes Yes thankyou, it now works, I understand now thankyou very much :-)
0

#4 User is offline   ElanMan 

  • In, out, shake it all about...
  • PipPipPipPipPip
  • View gallery
  • Group: Members
  • Posts: 3,298
  • Joined: 11-March 08
  • Reputation: 54
  • Gender:Male
  • Location:Darlington
  • Experience:Nothing
  • Area of Expertise:Nothing

Posted 02 August 2009 - 07:30 PM

You could also get rid of the else if conditionals. Only when readyState == 4, change the visibility.
function loading(connection) {
        var timer = setTimeout(function() { loading(connection); }, 1);
        var objLoadingImg = document.getElementById('loading-image');
        objLoadingImg.style.visibility = 'visible';
        if(connection.readyState == 4) {
                objLoadingImg.style.visibility = 'hidden';
                clearTimeout(timer);
        }
}

0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users