Jump to content

Ajax, xmlHTTP not recognized as an object


george

Recommended Posts

I run this, and get a xmlHttp is not an object error. I am at my witz end

// JavaScript Document	var xmlHttp;	function startup() {		var xmlHttp;		xmlHttp=GetXmlHttpObject();		xmlHttp.onreadystatechange=stateChanged;		xmlHttp.open('POST','bringmethedata.php',true);		xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");		xmlHttp.send();		}				function stateChanged() {			if (xmlHttp.readyState==4) {				document.getElementById('letPHPdothework').innerHTML = xmlHttp.responseText;				}		}	function GetXmlHttpObject() {	  var xmlHttp=null;	  try		{		// Firefox, Opera 8.0+, Safari		xmlHttp=new XMLHttpRequest();		}	  catch (e)		{		// Internet Explorer		try		  {		  xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");		  }		catch (e)		  {		  xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");		  }		}	return xmlHttp;	}

Link to comment
Share on other sites

There are three distinct variables in your code named "xmlHttp".1) the global xmlHttp variable declared on the first line of code.2) the local variable inside your startup() function.3) the local variable inside your GetXmlHttpObject() function.What's happening is that you declare a variable called xmlHttp (1) and then, at some point, startup() fires. This function creates a new xmlHttp variable (2) and instantiates that variable as an XMLHttp object. The function also sets up the readystatechange handler called stateChanged(). When stateChanged() fires, it is referencing that global xmlHttp variable (1) that has never been instantiated. Hence, "xmlHttp is not an object".Try this instead:

var xmlHttp;function startup(){	xmlHttp = GetXmlHttpObject();	...

Link to comment
Share on other sites

Automation server can't create object
That is what my computter is telling me. and it seems to be happening in the error trapping area that tries to create the xmlHTTP object.
function GetXmlHttpObject() {	  var xmlHttp=null;	  try		{		// Firefox, Opera 8.0+, Safari		xmlHttp= XMLHttpRequest();		}	  catch (e)		{		// Internet Explorer		try		  {		  xmlHttp= ActiveXObject("Msxml2.XMLHTTP");		  }		catch (e)		  {		  xmlHttp= ActiveXObject("Microsoft.XMLHTTP");		  }		}	return xmlHttp;	}

Link to comment
Share on other sites

This function works for me, you might try it:

function GetXmlHttpObject(){	var object = false;		if(window.XMLHttpRequest)	{		object = new XMLHttpRequest();	}	else if(window.ActiveXObject)	{		try		{			object = new ActiveXObject("Msxml2.XMLHTTP");		}		catch(e)		{			try			{				object = new ActiveXObject("Microsoft.XMLHTTP");			}			catch(e)			{			}		}	}		return object;}

Link to comment
Share on other sites

function XHR() {   var _handler;   if(window.XMLHttpRequest) {	  _handler = new XMLHttpRequest();    } else if(window.ActiveXObject) {	  try {		 _handler = new ActiveXObject("Msxml2.XMLHTTP");	  } catch (e) {		 _handler = new ActiveXObject("Microsoft.XMLHTTP");	  }   }   return _handler;}function startup() {   var xmlHttp;   xmlHttp = XHR();   xmlHttp.onreadystatechange = stateChanged;   xmlHttp.open("POST", "bringmethedata.php", true);   xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");   xmlHttp.send("");}		function stateChanged() {   if(xmlHttp.readyState == 4 && xmlHttp.status == 200) {	  document.getElementById("letPHPdothework").innerHTML = xmlHttp.responseText;	   }}

Why are use using the POST method, yet sending no parameters? o.o

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...