Ajax ResponseXml返回的xml是空值
我写了一个Ajax个程序,可是返回xml时老是空值,请哪位高手给解决一下!!!小弟感激不尽!!!程序如下页面:functionajaxFunction(){varurl=...
我写了 一个Ajax个程序,可是返回xml时老是空值,请哪位高手给解决一下!!!小弟 感激不尽!!!程序如下
页面:
function ajaxFunction()
{
var url = "../wis/ajaxTest1.action?funcCode=${funcCode}";
if (window.XMLHttpRequest) {
xmlRequest = new XMLHttpRequest();
}else if (window.ActiveXObject) {
xmlRequest = new ActiveXObject("Microsoft.XMLHTTP");
}
if(xmlRequest){
xmlRequest.open("GET", url, true);
xmlRequest.onreadystatechange = getDeptItems;
xmlRequest.send(null);
}
}
function getDeptItems() {
if (xmlRequest.readyState == 4) {
if (xmlRequest.status == 200) {
//window.myForm.time.value=xmlRequest.responseText;
var employees = xmlRequest.responseXML.getElementsByTagName("employees")[0];
alert(employees);
//alert(employees.childNodes.length );
}
}
}
后台:
public void ajaxTest()
{
HttpServletResponse response =ServletActionContext.getResponse();
// Date date=new Date();
// SimpleDateFormat s=new SimpleDateFormat("yyyy年MM月dd日");
// String d=s.format(date);
try {
StringBuffer sb=new StringBuffer();
sb.append("<employee>");
sb.append("<id>"+1+ "</id>");
sb.append("<firstName>" + 2 + "</firstName>");
sb.append("<lastName>" + 3 + "</lastName>");
sb.append("</employee>");
response.setContentType("text/xml");
response.setHeader("Cache-Control", "no-cache");
response.getWriter().write("<employees>" + sb.toString() + "</employees>");
} catch (Exception e1) {
e1.printStackTrace();
}
}
不知为什么 页面的employees总是null 展开
页面:
function ajaxFunction()
{
var url = "../wis/ajaxTest1.action?funcCode=${funcCode}";
if (window.XMLHttpRequest) {
xmlRequest = new XMLHttpRequest();
}else if (window.ActiveXObject) {
xmlRequest = new ActiveXObject("Microsoft.XMLHTTP");
}
if(xmlRequest){
xmlRequest.open("GET", url, true);
xmlRequest.onreadystatechange = getDeptItems;
xmlRequest.send(null);
}
}
function getDeptItems() {
if (xmlRequest.readyState == 4) {
if (xmlRequest.status == 200) {
//window.myForm.time.value=xmlRequest.responseText;
var employees = xmlRequest.responseXML.getElementsByTagName("employees")[0];
alert(employees);
//alert(employees.childNodes.length );
}
}
}
后台:
public void ajaxTest()
{
HttpServletResponse response =ServletActionContext.getResponse();
// Date date=new Date();
// SimpleDateFormat s=new SimpleDateFormat("yyyy年MM月dd日");
// String d=s.format(date);
try {
StringBuffer sb=new StringBuffer();
sb.append("<employee>");
sb.append("<id>"+1+ "</id>");
sb.append("<firstName>" + 2 + "</firstName>");
sb.append("<lastName>" + 3 + "</lastName>");
sb.append("</employee>");
response.setContentType("text/xml");
response.setHeader("Cache-Control", "no-cache");
response.getWriter().write("<employees>" + sb.toString() + "</employees>");
} catch (Exception e1) {
e1.printStackTrace();
}
}
不知为什么 页面的employees总是null 展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询