req.responseXML.documentElement总是为NULL

szbszb 2007-11-21 04:29:40
我做的JSP页面上无法获得ajax从后台传来的数据,req.responseXML.documentElement总是为NULL,请大家帮我看看:
<SCRIPT type="text/javascript">
var req;
window.onload=function(){
}

function change(id)
{
var url= "<%=host%>/task/TaskAction.do?operate=getidst&id="+id;
if(window.XMLHttpRequest)
{
req=new XMLHttpRequest();
}else if(window.ActiveXObject)
{
req=new ActiveXObject("Microsoft.XMLHTTP");
}

if(req)
{
req.open("GET",url,true);
req.onreadystatechange=callback;
req.send(null);
}
}

function callback()
{
if(req.readyState == 4)
{
if(req.status == 200)
{
parseMessage();
}else{
alert("Not able to retrieve description"+req.statusText);
}
}
}

function parseMessage()
{
var xmlDoc=req.responseXML.documentElement;//这是总是为NULL
var xSel=xmlDoc.getElementsByTagName('select');//无法获得ajax取得的结果
var select_root=document.getElementById('idst');
select_root.options.length=0;

for(var i=0;i<xSel.length;i++)
{
var xValue=xSel[i].childNodes[0].firstChild.nodeValue;
var xText=xSel[i].childNodes[1].firstChild.nodeValue;
var option=new Option(xText,xValue);
try{
select_root.add(option);
}catch(e){
}
}


}
</SCRIPT>



Action:

public ActionForward getidst(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) throws Exception{
TaskForm taskForm = (TaskForm)form;
//获得任务类型
String id = request.getParameter("id");
List idstlist = idsts.getIndustrySubTypeajax(id);
request.setAttribute("idstlist",idstlist);
response.setContentType("text/xml");
response.setHeader("Cache-Control","no-cache");
String xml_start="<?xml version=\"1.0\" encoding=\"utf-8\"?>";
xml_start+="<selects>";
String xml_end="</selects>";
String xml="<select>";
String m_lbbm_se="";
String m_lbmc_se="";
Iterator it=idstlist.iterator();
while(it.hasNext()){
IndustrySubType indutrySubType=(IndustrySubType)it.next();
m_lbbm_se=indutrySubType.getSubTypeId().toString();
m_lbmc_se=indutrySubType.getSubTypeName();
xml +="<select><value>"+m_lbbm_se+"</value><text>"+m_lbmc_se+"</text></select>";
}
String last_xml=xml_start+xml+xml_end;
response.getWriter().write(last_xml);
return mapping.findForward("task_add");
}

打印出来的last_xml:
<?xml version="1.0" encoding="utf-8"?><selects><select><value>0</value><text>请选择</text></select><select><value>9</value><text>工作</text></select><select><value>10</value><text>学习</text></select></selects>



...全文
432 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
amangoing 2008-12-25
  • 打赏
  • 举报
回复
阿群 2008-07-14
  • 打赏
  • 举报
回复
我碰到这样的问题,正在寻找解决方法,responseText是有值的,但responseXML.xml为空,responseXML.responseElement为NULL,哪位大虾提供些经验!
西安风影 2007-11-21
  • 打赏
  • 举报
回复
你在服务器端跟踪一下这个变量last_xml,看是否是一个合格的xml文件
保证只有一个跟元素
Go 旅城通票 2007-11-21
  • 打赏
  • 举报
回复
var xmlDoc=req.responseXML.documentElement;//这是总是为NULL

这个documentElement不需要也可以,直接

var xmlDoc=req.responseXML;

就行了

52,797

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 Ajax
社区管理员
  • Ajax
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧