This is how i use Ajax, any better ideas?

This will be done in a javascript

#Creates an xml object

/*Ajax Code Starts Here*/

var XmlReq;

function CreateXmlReq()

{

try

{

XmlReq =new ActiveXObject("Msxml2.XMLHTTP");

}

catch(e)

{

try

{

XmlReq =new ActiveXObject("Microsoft.XMLHTTP");

}

catch(oc)

{

XmlReq =null;

}

}

if(!XmlReq && typeof XMLHttpRequest !="undefined")

{

XmlReq =new XMLHttpRequest();

}

}

#Response Handler

function HandleResponse()

{

if(XmlReq.readyState == 4)

{

if(XmlReq.status == 200)

{

rootNode=XmlReq.responseXML.documentElement

var tables=rootNode.getElementsByTagName("Rows")

}

}

}

#Sending request to server using Ajax

function sentToServer(id)

{

CreateXmlReq();

if(XmlReq)

{

XmlReq.onreadystatechange = HandleResponse;

var Random=parseInt(Math.random()*99999999);

XmlReq.open("GET","MyPage.jsp?ajx=1&id="+id +"&rand="+Random,true);

XmlReq.send();

}

}

Message was edited by:

xema

[2406 byte] By [xemaa] at [2007-11-27 5:59:10]
# 1
your way works fine if you do only one ajax request at a time, but when you start doing multiple requests at once this code will break because the XmlReq will be overwritten so you can't read the status of a previous request anymore. I use an array to store different Ajax handlers.
gimbal2a at 2007-7-12 16:35:16 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...