Jump to content

AJAX Call from Javascript Not Working Properly


buckibooster

Recommended Posts

function getXMLHttp(){	var xmlHttp;	try	{		//Firefox, Opera 8.0+, Safari		xmlHttp = new XMLHttpRequest();	}	catch(e)	{		//Internet Explorer		try		{			xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");		}		catch(e)		{			try			{				xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");			}			catch(e)			{				alert("Your browser does not support AJAX!")				return false;			}		}	}	return xmlHttp;}function populateEntries(menu, userName, entryRow){	var xmlHttp = getXMLHttp();	xmlHttp.onreadystatechange = function()	{		if ((xmlHttp.readyState == 4) && (xmlHttp.status == 200) && (xmlHttp.responseText == ""))		{			window.alert("There are no records to view!");		}		else if ((xmlHttp.readyState == 4) && (xmlHttp.status == 200))		{			var response = xmlHttp.responseText;//			window.alert("PHP Response = '"+response+"'");		}	}	xmlHttp.open("GET", "getUsers.php", true);	xmlHttp.send(null);//	ADDITIONAL UNRELATED CODE THAT PARSES THE “RESPONSE,” CREATING A MULTIDIMIENSIONAL ARRAY, WHICH POPULATES A SELECT INPUT FOLLOWS ....}

I am using an AJAX call from a javascript to populate a SELECT input. This script is not working as shown in the code excerpt below. However, when I insert an alert of any type at the location shown by the comment, it works as planned. What could be causing this? I've also tried, without success, inserting a delay function after the AJAX call to allow time for the response to be returned (even with the readystate and status tests shown). Any suggestions as to how I can get this to work as desired?

Edited by buckibooster
Link to comment
Share on other sites

You have to deal with the response inside the onreadystatechange event handler. The variable response is local to that function and is not accessible from outside.

Link to comment
Share on other sites

If you see the response text appear in the alert then this code is working also, so I don't see how you can conclude that the other code is "unrelated."

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
×
×
  • Create New...