Hi,
I want to populate dependednt drop down boxes using Ajax. I am using dom4j
to build the xml response that I want to send.
I have the following in My Action.
DynaActionForm dynaDropDownForm = (DynaActionForm) form;
String selectedVal = (String) dynaDropDownForm.get
("selectedVal");
String selectedList = (String) dynaDropDownForm.get
("selectedList");
System.err.println(
"Selected Val:"
+ selectedVal
+ " Selected list:"
+ selectedList);
Document document = DocumentHelper.createDocument();
Element root = document.addElement("options");
for (int i = 0; i < 5; i++) {
Element optionElement = root.addElement("option");
optionElement.addAttribute("value", "" + i);
optionElement.addAttribute("text", "Option " + i);
}
PrintWriter out = response.getWriter();
OutputFormat outputFormat = OutputFormat.createCompactFormat();
XMLWriter writer = new XMLWriter(out, outputFormat);
writer.write(document);
writer.flush();
return null;
My javascript function is as follows
var req;
function changed(which,list) {
var url="<%=request.getContextPath()%>/MyProj/getList.do?";
var selectedVal = list.options[list.selectedIndex].value;
if(window.XmlHttpRequest) {
req = new XmlHttpRequest();
}else if(window.ActiveXObject) {
req=new ActiveXObject("Microsoft.XMLHTTP");
}
if(req==null) {
alert("Browser Doesnt Support AJAX");
return;
}
req.onreadystatechange = function() {
populate(which);
}
url = url + "selectedList=" + (which-1) + "&selectedVal=" + selectedVal;
alert(url);
req.open("GET",url,true);
req.send(null);
}
function populate(which) {
if (req.readyState==4 || req.readyState=="complete") {
alert('Got Data' + req.responseText);
var xmldoc = req.responseXML.documentElement;
if(xmldoc == null) {
alert("No Data");
return;
}
var options = xmldoc.getElementsByTagName();
var targetList = document.getElementById("select_" + which);
for(i=0;i<options.length;i++) {
targetList.options[i+1] = new Option(options.getAttribute
("text"),options.getAttribute("value"));
}
}
}
The problem is that I am not getting a proper response from server.. What i
get in my responseText is following.
<options>
<option value="0" text="Option 0"/>
<option value="1" text="Option 1"/>
<option value="2" text="Option 3"/>
<option value="3" text="Option 3"/>
<option value="4" text="Option 4"/>
</options>
Error 500: Server Caught Unhandled exception from servlet [action]: null
SO this Error 500 is what prevents me from parsing the XML. I am using
Websphere 5.1 with struts 1.1
ANother Strange Problem is in on my WSAD console i dont get any exception
stack trace, not even the sysouts that I have put in.
ANy clues??
--
Puneet