I'm trying to write a script page that uses the xmlHTTP to an aspx page that then sends back some XML which I can then use the javascript to read and populate a dropdown list.
I seem to be running into the problem of the returned XML (or lack there of) If I do a responseText, all is well cept the response is not formatted as XML just a large string.
Here is the Javascript
1<script type="text/javascript">2 /* Create a new XMLHttpRequest object to talk to the Web server */3 var xmlHttp = false;45 function CreateXmlHttp()6 {7 /*@dotnet.itags.org.cc_on @dotnet.itags.org.*/8 /*@dotnet.itags.org.if (@dotnet.itags.org._jscript_version >= 5)910 try11 {12 xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");13 } catch (e)14 {15 try16 {17 xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");18 }19 catch (e2)20 {21 xmlHttp = false;22 }23 }24 @dotnet.itags.org.end @dotnet.itags.org.*/2526 if (!xmlHttp && typeof XMLHttpRequest != 'undefined')27 {28 xmlHttp = new XMLHttpRequest();29 }30 }313233 function callServ()34 {3536 var site;37 var siteddl = document.getElementById('ddlSites');38 for (var i = 0; i < siteddl.options.length; i++)39 {40 if (siteddl.options[i].selected)41 {42 site = siteddl.options[i].value;43 //document.write(site);44 }45 }46 if (site == '-1')47 {48 return;49 }50 else51 {5253 CreateXmlHttp();5455 var url="popddls.aspx?w=1&id=" + escape(site);565758 xmlHttp.open("GET", url, true);5960 xmlHttp.setRequestHeader("Content-Type", "text/xml");6162 xmlHttp.onreadystatechange = updatePage;6364 xmlHttp.send(null);65 }6667 }6869 function updatePage()70 {71 if(xmlHttp.readyState ==4)72 {73 var response1 = xmlHttp.responseXML;7475 var ddlGrades = document.getElementById('ddlGrades');7677 var grades = response1.getElementsByTagName("grade");78 //ddlGrades.options.length = 0;79 for (var x=0; x < grades.length; x++)80 {81 var id = grades[x].childNodes[0].value;82 var gradedate = grades[x].childNodes[1].value;83 document.write(id + " " + gradedate);84 var newOption = new option();85 newOption.text = gradedate;86 newOption.valueOf = id;87 ddlGrades.options[ddlGrades.length] = newOption;88 }8990 }91 }929394 </script>
Here is the code for popddl which if I run it, it does write the correct XML
1Sub load_gradesddl()2Dim idAs Integer = Request.QueryString("id")3Dim sqlAs String ="select id,gradedate from grades where siteid=" & id4Dim dsAs System.Data.DataSet = DataFunc.FillDS(sql,"gcioq")56 Response.Write("<?xml version=""1.0"" encoding=""utf-8"" ?>")7 Response.Write("<grades>")8For IAs Integer = 0To ds.Tables(0).Rows.Count - 1910 Response.Write("<grade>")11 Response.Write("<id>" & ds.Tables(0).Rows(I).Item("id") &"</id>")12 Response.Write("<gradedate>" & ds.Tables(0).Rows(I).Item("gradedate") &"</gradedate>")13 Response.Write("</grade>")14Next15 Response.Write("</grades>")16End Sub
the error I get in firefox is response1 has no properties. in IE7 I do not get an error but it doesn't work.
What am I doing wrong here?
EDIT:
I also noticed if I do a document.write(grades.length) I get a Zero back though I know the popddl is generating correct XML.
Ok, I got it to work but only in IE7, in firefox it get the right amound of nodes but everything comes out as undefined.
using this to get the data
var id = grades[x].childNodes[0].firstChild.text;var gradedate = grades[x].childNodes[1].firstChild.text;
Also, this is the XML output
<?xml version="1.0" encoding="utf-8"?>
-<grades>
-<grade>
<id>11</id>
<gradedate>11/27/2007</gradedate>
</grade>
- <grade>
<id>18</id>
<gradedate>11/27/2006</gradedate>
</grade>
-<grade>
<id>19</id>
<gradedate>11/28/2007</gradedate>
</grade>
-<grade>
<id>20</id>
<gradedate>11/29/2007</gradedate>
</grade>
-<grade>
<id>21</id>
<gradedate>11/30/2007</gradedate>
</grade>
</grades>
Ok, I got it to work but only in IE7, in firefox it get the right amound of nodes but everything comes out as undefined.
using this to get the data
var id = grades[x].childNodes[0].firstChild.text;var gradedate = grades[x].childNodes[1].firstChild.text;
Also, this is the XML output
<?xml version="1.0" encoding="utf-8"?>
-<grades>
-<grade>
<id>11</id>
<gradedate>11/27/2007</gradedate>
</grade>
- <grade>
<id>18</id>
<gradedate>11/27/2006</gradedate>
</grade>
-<grade>
<id>19</id>
<gradedate>11/28/2007</gradedate>
</grade>
-<grade>
<id>20</id>
<gradedate>11/29/2007</gradedate>
</grade>
-<grade>
<id>21</id>
<gradedate>11/30/2007</gradedate>
</grade>
</grades>
Hi,
Thank you or your post!
I suggest you try this for firefox:
var id = grades[x].childNodes[0].firstChild.nodeValue;var gradedate = grades[x].childNodes[1].firstChild.nodeValue;
If you have further questions,let me know.
Best Regards,
No comments:
Post a Comment