jnutty1 Posted January 23, 2007 Share Posted January 23, 2007 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. Link to comment Share on other sites More sharing options...
jesh Posted January 23, 2007 Share Posted January 23, 2007 The way I handled this was to use a private variable (without the this keyword) to store my httpRequest object inside my AJAX object and I used an internal function to handle the readystatechange event (to which I could pass a reference to a separate function to handle the response): function sendData(smethod,surl,sdata,basync,index,callback){ this.GetXmlHttpObject=GetXmlHttpObject; this.index=index; var xmlHttp=this.GetXmlHttpObject() ... xmlHttp.onreadystatechange=function(){ HandleAjaxRequest(callback); } function HandleAjaxRequest(callback) { if(xmlHttp.readyState == 4) { callback(xmlHttp); } } ...} Then, you could use it like this: function myCallbackHandler(response){ alert(response.responseText);}var test = new sendData('POST','pagea.php','id=1',true,0,myCallbackHandler); Hope it helps! Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now