Jump to content
Sign in to follow this  
buckibooster

AJAX Call from Javascript Not Working Properly

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

Share this post


Link to post
Share on other sites

I am using an AJAX call from a javascript to populate a SELECT input.

 

 

Where is that code?

Share this post


Link to post
Share on other sites

Moderator:

 

That part of the code is working and unrelated to the AJAX call problem. I choose to protect that code with the "additional unrelated code that ..." statement at the end of my code snippet.

Share this post


Link to post
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.

Share this post


Link to post
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."

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this  

×
×
  • Create New...