首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > Ajax >

AJAX里req.responseXML.documentElement返回null的有关问题

2012-02-20 
AJAX里req.responseXML.documentElement返回null的问题 - Web 开发 / Ajax在做一个老项目,没办法用DWR,重

AJAX里req.responseXML.documentElement返回null的问题 - Web 开发 / Ajax
在做一个老项目,没办法用DWR,重新拿起了ajax。
在网上找的代码,结果后台servlet里面是可以调用到的,并且有值。但是到前台返回的时候,req.responseXML.documentElement是空的。
已经尝试过调整编码格式,writer.close()方法和writer.flush()方法了。但是确实取不到返回值。
代码如下

index.jsp
<%@ page language="java" contentType="text/html; charset=utf-8"%>

<html>
  <head>
  <title>My JSP 'index.jsp' starting page</title>
  <http-equiv="Content-Type" content="text/html; charset=utf-8">
  <script type="text/javascript">
  var req;
  window.onload=function(){//页面加载时的函数

  }
   
  function Change_Select(){//当第一个下拉框的选项发生改变时调用该函数
  var zhi = document.getElementById("hero").options[document.getElementById("hero").selectedIndex].value;
  alert(zhi);
  var url = "select?id="+escape(zhi);
  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; //指定回调函数为callback
  req.send(null);
  }
  }
   
  function callback(){
  if(req.readyState ==4){
  if(req.status ==200){
  parseMessage();//解析XML文档
  }else{
  alert(req.status);
  alert("Not able to retrieve description" + req.statusText);
  }
  }
  }
   
  function parseMessage(){
  var xmlDoc = req.responseXML.documentElement;//获得返回的XML文档
  var xSel = xmlDoc.getElementsByTagName('select');
  //获得XML文档中的所有<select>标记
  var select_root = document.getElementById('skill');
  //获得网页中的第二个下拉框
  select_root.options.length=0;
  //每次获得新的数据的时候先把每二个下拉框架的长度清0
   
  for(var i=0;i<xSel.length;i++){
  var xValue = xSel[i].childNodes[0].firstChild.nodeValue;
  //获得每个<select>标记中的第一个标记的值,也就是<value>标记的值
  var xText = xSel[i].childNodes[1].firstChild.nodeValue;
  //获得每个<select>标记中的第二个标记的值,也就是<text>标记的值
   
  var option = new Option(xText, xValue);
  //根据每组value和text标记的值创建一个option对象
   
  try{
  select_root.add(option);//将option对象添加到第二个下拉框中
  }catch(e){
  }
  }
  }  
  </script>
  </head>
  
  <body>
  <div align="center">
  <form name="form1" method="post" action="">
  <TABLE width="70%" boder="0" cellspacing="0">
  <TR>
  <TD align="center">Double Select Box</TD>
  </TR>
  <TR>
  <TD>
  <SELECT name="hero" id="hero" onChange="Change_Select()">
  <OPTION ="0" value="0">Unbounded</OPTION>
  <OPTION ="1" value="1">D.K.</OPTION>
  <OPTION ="2" value="2">NEC.</OPTION>


  <OPTION ="3" value="3">BOSS</OPTION>
  </SELECT>
  <SELECT name="skill" id="skill">
  <OPTION ="0">Unbounded</OPTION>
  </SELECT>
  </TD>
  </TR>
  <TR><td>&nbsp;</td></TR>
  </TABLE>
  </form>
  </div>
  </body>
</html>


SelectServlet.java
package com;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class SelectServlet extends HttpServlet {

  /**
  * Constructor of the object.
  */
  public SelectServlet() {
  super();
  }

  /**
  * Destruction of the servlet. <br>
  */
  public void destroy() {
  super.destroy(); // Just puts "destroy" string in log
  // Put your code here
  }

  /**
  * The doGet method of the servlet. <br>
  *
  * This method is called when a form has its tag method equals to get.
  * 
  * @param request the request send by the client to the server
  * @param response the response send by the server to the client
  * @throws ServletException if an error occurred
  * @throws IOException if an error occurred
  */
  public void doGet(HttpServletRequest request, HttpServletResponse response)
  throws ServletException, IOException {
  response.setContentType("text/xml;utf-8");
  response.setHeader("Cache-Control","no-cache");
  String targetId=request.getParameter("id").toString();
  String xml_start="<selects>";
  String xml_end="</selects>";
  String xml="";
  System.out.println("------------------targetId------>>>"+targetId);
  if(targetId.equalsIgnoreCase("0")){
  xml="<select><>0</><text>Unbounded</text></select>";
  }else if(targetId.equalsIgnoreCase("1")){
  System.out.println("------------------->>>"+targetId);
  xml="<select><>1</><text>Mana Burn</text></select>";
  xml +="<select><>2</><text>Death Coil</text></select>";
  xml +="<select><>3</><text>Unholy Aura</text></select>";
  xml +="<select><>4</><text>Unholy Fire</text></select>";
  }else if(targetId.equalsIgnoreCase("2")){
  xml="<select><>1</><text>Corprxplode</text></select>";
  xml +="<select><>2</><text>Raise Dead</text></select>";
  xml +="<select><>3</><text>Brilliance Aura</text></select>";
  xml +="<select><>4</><text>Aim Aura</text></select>";


  }else{
  xml="<select><>1</><text>Rain of Chaos</text></select>";
  xml +="<select><>2</><text>Finger of Death</text></select>";
  xml +="<select><>3</><text>Bash</text></select>";
  xml +="<select><>4</><text>Summon Doom</text></select>";
  }
  String last_xml=xml_start+xml+xml_end;
  System.out.println("--------------------><"+last_xml);
  PrintWriter writer = response.getWriter();
  writer.write(last_xml);
  writer.flush();
  writer.close();
  }

  /**
  * The doPost method of the servlet. <br>
  *
  * This method is called when a form has its tag method equals to post.
  * 
  * @param request the request send by the client to the server
  * @param response the response send by the server to the client
  * @throws ServletException if an error occurred
  * @throws IOException if an error occurred
  */
  public void doPost(HttpServletRequest request, HttpServletResponse response)
  throws ServletException, IOException {

  doGet(request,response);
  }

  /**
  * Initialization of the servlet. <br>
  *
  * @throws ServletException if an error occure
  */
  public void init() throws ServletException {
  // Put your code here
  }

}


web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4" 
  xmlns="http://java.sun.com/xml/ns/j2ee" 
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
  xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 
  http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
  <servlet>
  <servlet-name>SelectServlet</servlet-name>
  <servlet-class>com.SelectServlet</servlet-class>
  </servlet>

  <servlet-mapping>
  <servlet-name>SelectServlet</servlet-name>
  <url-pattern>/select</url-pattern>
  </servlet-mapping>
   
  <welcome-file-list>
  <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>

哪位大大帮我看看啊

[解决办法]

HTML code
 <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> 


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>My JSP 'LianDongJsp.jsp' starting page </title>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8">
<script type="text/javascript">
var req;
window.onload = function(){
}
function Change_Select(){
var zhi=document.getElementById("hero").value;
var url="select?id="+escape(zhi);
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;
var xSel = xmlDoc.getElementsByTagName('select');
var select_root = document.getElementById('skill');
select_root.options.length = 0;
for(var i=0; i <xSel.length;i++){
var xValue = xSel[i].childNodes[1].firstChild.nodeValue;
var xText = xSel[i].childNodes[3].firstChild.nodeValue;
var option = new Option(xText,xValue);
try{
select_root.options.add(option);
}catch(e){
}
}
}
</script>
</head>

<body>
<div align="center">
<form name="form1" method="post" action="">
<table width="70%" boder="0" cellspacing="0">
<tr>
<td align="center">
Double Select Box
</td>
</tr>
<tr>
<td>
<select name="hero" id="hero" onchange="Change_Select()">
<option value="0">
Unbounded
</option>
<option value="1" selected="selected">
D.K
</option>
<option value="2">
NEC.
</option>
<option value="3">
BOSS
</option>
</select>
<select name="skill" id="skill">
<option value="0">
Unbounded
</option>
</select>
</td>
</tr>
<tr>
<td>
&nbsp;
</td>
</tr>
</table>
</form>
</div>
</body>
</html>

热点排行