小服务程序响应:
for (int i = 0; i < projNames.size(); i++) {
sb.append("<project>");
sb.append("<projectName>" + projNames.get(i) + "</projectName>");
sb.append("</project>");
res.setContentType("text/xml");
res.setHeader("Cache-Control", "no-cache");
System.out.println(" test="+test);
res.getWriter().write(sb.toString());
}
System.out.println 将“sb”值显示为
<project><projectName>sample</projectName></project><project><projectName>sample 2</projectName></project>
我正在尝试检索响应 XML,但是当我检查长度时“对象值为 null”(在 IE 8 中调试)
function displayValues()
{
var response;
if (req.responseXML != null)
alert("res xml is ther=="+ response); //displays UNDEFINED
else
alert("no res xml");
response = req.responseXML.getElementsByTagName("project")[0];
alert("child node"+response.childNodes.length); // alert is not displayed
}
请赐教。谢谢
AJAX 代码:
<SCRIPT type="text/javascript">
var response;
var firstName;
var icdatevalue;
function getProject()
{
var url ="DBActionServlet?action=getProject";
initRequest(url);
req.onreadystatechange = processRequest;
req.open("POST", url, true);
req.send(null);
}
function initRequest(url)
{
if (window.XMLHttpRequest)
{
req = new XMLHttpRequest();
}
else if (window.ActiveXObject)
{
isIE = true;
req = new ActiveXObject("Microsoft.XMLHTTP");
}
}
function processRequest()
{
if (req.readyState == 4)
{
if (req.status == 200)
{
alert("status="+req.status);//status is shown as 200
displayValues(); //this function code mentioned above
}
}
}
</SCRIPT>