Hi,I have a problem with creating an AJAX object. I took the code that is in the AJAX section of this site and it worked just fine. But, if I create an object of the same code (see below) the onreadystatechange function doesn't seem to recognize elements in the object.
function sendData(smethod,surl,sdata,basync,index){ this.GetXmlHttpObject=GetXmlHttpObject; this.index=index; this.xmlHttp=this.GetXmlHttpObject() if (this.xmlHttp==null) { alert ("Browser does not support HTTP Request") } if (smethod.toUpperCase()=="GET" && sdata.length>0){ surl+="?"+sdata; sdata=null; } this.xmlHttp.onreadystatechange=function(){ if (this.xmlHttp.readyState==4 || this.xmlHttp.readyState=="complete") { doResponse(this.xmlHttp.responseText,this.index); } } this.xmlHttp.open(smethod,surl,basync);this.xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");this.xmlHttp.send(sdata);} 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) { try { xmlHttp=new ActiveXObject("Microsoft.XMLHTTP"); } catch (e) { //alert("Your browser does not support AJAX!"); } } }return xmlHttp;}
The javascript code to create the objects would be as follows:
var aj=new Array();function test(){ aj[0]=new sendData('POST','pagea.php','id=1',true,0) aj[1]=new sendData('POST','pageb.php','id=2',true,1)}function doResponse(s,index){ do something with s. aj[index]=null;}
Each time a sendData object is created and the onreadystatechange function executes, an error stating that this.xmlHttp.readyState is null or not an object (in IE) or it doesn't have any properties (in netscape, firefox).Any thought would be appreciated.