This week's book giveaway is in the General Computing forum. We're giving away four copies of Arduino in Action and have Martin Evans, Joshua Noble, and Jordan Hochenbaum on-line! See this thread for details.
Hi I have just started with ajax. I am trying to build an application for autocompletion. We enter a lettr and names starting with that letter should be displayed in div tag.
I am getting correct response but am not able to parse it to show it in the div tag. I think I am not using getElementsByTagName correctly.
function getEmpAutoData(emptxt){ var divObj=document.getElementById("newDiv"); var empChar=emptxt.value; alert("empchar==>"+empChar); //var params=encodeURI("hidden=empauto&emp="+empChar); var url="TraditionalAjaxController?hidden=empauto&emp="+empChar; var req=initRequest(); req.onreadystatechange = function() { if (req.readyState == 4) { if (req.status == 200) {
//if response comes correctly..(if no exception in middle of processing) parseMessageAuto(req.responseXML);// Is this correct ? } }else{ //This is for progress image...indicator.gif is image for loading image ..which will rotate continously divObj.style.visibility="visible";
var responseNodes=resXML.getElementsByTagName("message")[0]; alert("response node===>"+responseNodes); // here it fails the length is 0 here I cannot proceed further if(responseNodes.length>0){ var responseNode=responseNodes[0]; alert("responseNode" + responseNode); var itemNodes=responseNode.getElementsByTagName("item"); for(var i=0;i<itemNodes[i].length;i++){ var nameNodes=itemNodes[i].getElementsByTagName("name"); //getting the elements with tagname as <name> from xml response var valueNodes=itemNodes[i].getElementsByTagName("value"); //getting the elements with tagname as <value> from xml response if(nameNodes.length>0 && valueNodes.length>0){ var name=nameNodes.childNodes[0].nodeValue;//represents ID of employee var value1=valueNodes.childNodes[0].nodeValue;//represents Name of employee
Step 4 might point it out for you. I think you are missing one important line on top of your XML document.
Eric
Parul Aggarwal
Greenhorn
Joined: Mar 27, 2008
Posts: 9
posted
0
Thanks Eric, I'll take care.
Could you please explain me Point 4 with some example, a psuedo code.
I cant identify which important line I m missing from my generated xml document and how to open it directly into the browser.
Please be patient with me. Thanks
Eric Pascarello
author
Rancher
Joined: Nov 08, 2001
Posts: 15362
6
posted
0
If you are using a post to open it up, you probably need to just hard code some values on the server. You than type the direct addy to the file that the Ajax call calls.
I do not see the <xml..... line
Eric
Parul Aggarwal
Greenhorn
Joined: Mar 27, 2008
Posts: 9
posted
0
Hi Eric
This is what I get as response. There is no syntax error whether I write <?xml version="1.0" encoding="UTF-8" ?> or not.
However when I start my Tomcat Ver 5.5.26, then on my tomcat console during initialization it shows "org.apache.catalina.core.StandardHost start INFO: XML validation disabled. Can this be the reason that I am not able to parse the result.
Parul
Parul Aggarwal
Greenhorn
Joined: Mar 27, 2008
Posts: 9
posted
0
continued ...
If XML validation disabled is the problem then pls tell me the solution
Also I am giving the code in proper format. please see..
Once you got the responseXML object, then get the root element from that as shown in below.. var root=resXML.documentElement; then using the "root" element you can get root/other elements by their tag name as follows..... var responseNodes=root.getElementsByTagName("message")[0];
I think this may help you...
regards, Raj.S
Parul Aggarwal
Greenhorn
Joined: Mar 27, 2008
Posts: 9
posted
0
Hello
Raj. S - > I have tried this also var responseNodes=root.getElementsByTagName("message")[0]; however at
if(responseNodes.length>0)
statement it gives scripting error ---> responseNodes is null
If I remove [0] from it then also it doesn't pass the if test. i have placed alert also
I have tried my xml on both IE and Mozilla... I m badly struck..