Jump to content

Ajax Isn't Working Properly.


MrFish
 Share

Recommended Posts

I'm just making a simple ajax login and I want to display a progress bar at the different readystates instead of the generic circle-loading gif. Unfortunately when it is successful it only loads up to the readystate 2. When it is unsuccessful it only loads up to 1. I haven't gotten far, I'm only checking if the username exists (which works fine) but the images won't display correctly. Take a look here-ajaxerror.jpgPassword is not needed.

function ajaxLogin(){		username = document.getElementById('username').value;		password = document.getElementById('password').value;		responseimage = document.getElementById("ajaxprocessing");		ajaxRequest.onreadystatechange=function(){			if(ajaxRequest.readyState==1){				responseimage.src='images/loading1.gif';			}			if(ajaxRequest.readyState==2){				responseimage.src='images/loading2.gif';			}			if(ajaxRequest.readyState==3){				responseimage.src='images/loading3.gif';			}			if(ajaxRequest.readyState==4){				response = ajaxRequest.responseText;				if(response == "usernamefail"){					responseimage.src='images/error.gif';					alert('This username doesn\'t even exist.');				}			}		}			ajaxRequest.open("GET", "includes/login.php?username=" + username + "&password=" + password, "true");			ajaxRequest.send(null);	}

Link to comment
Share on other sites

The readyState property is not for tracking progress. The readyState gets set to 1 as soon as you call the open method. That's what a readyState of 1 means - that you've called open. It goes to 2 when you call send, so a readyState of 2 only means that you've called the send method. It doesn't mean that the request is halfway there, it means that you've called send. So readyState will go from 0 to 1 to 2 within a few milliseconds. A readyState of 3 will probably never happen, that means that part of the response, but not all of it, has been returned and is available. A readyState of 4 means that all of the response has been received and is available. So readyState is not for tracking progress. That's why people typically use a generic animated gif instead of trying to rely on readyState.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...