req.responseXML.documentElement总是为NULL
我做的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>