怎么解决Cannot read property 'getElementsByTagName' of null

珍惜最爱的你 2017-06-08 10:16:29
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page import="entity.*,java.util.List,dao.impl.*"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<script type="text/javascript" src="js/JSAjax.js"></script>
<script type="text/javascript" src="js/regist.js"></script>
<script type="text/javascript">
function showZy(){
var zys;
if(xmlHttp.readyState==4){
if(xmlHttp.status==200){
zys=xmlHttp.responseXML.getElementsByTagName("zy");
while(document.all.zy.options.length>0){
document.all.zy.removeChild(document.all.zy.childNodes[0]);
}
}
for(var i=0;i<zys.length;i++){
var option=document.createElement("OPTION");
option.text=zys[i].getElementsByTagName("zyname")[0].firstChild.data;
option.value=zys[i].getElementsByTagName("zyvalue")[0].firstChild.data;
document.all.zy.options.add(option);
}
}
}
function select() {
var p = document.getElementById("district").value;
createXMLHttpRequest();
xmlHttp.onreadystatechange = showZy;
xmlHttp.open("POST", "ZyServlet", true);
xmlHttp.setRequestHeader("Content-type",
"application/x-www-form-urlencoded");
xmlHttp.send("xy=" + p);
}
</script>




package servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.ResultSet;
import java.sql.SQLException;

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

import util.DBUtil;

/**
* Servlet implementation class ZyServlet
*/
@WebServlet("/ZyServlet")
public class ZyServlet extends HttpServlet {
private static final long serialVersionUID = 1L;

/**
* @see HttpServlet#HttpServlet()
*/
public ZyServlet() {
super();
// TODO Auto-generated constructor stub
}

/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
response.getWriter().append("Served at: ").append(request.getContextPath());
response.setContentType("text/xml;charset=utf-8");
String xyid=request.getParameter("xy");
PrintWriter out = response.getWriter();
String SQL_SELECT="SELECT * FROM ZY";
DBUtil db=new DBUtil();
db.getConnection();
String sql=SQL_SELECT+" where xyid=?";
ResultSet rs = db.executeQuery(sql, new String[] {xyid});
out.print("<response>");
try {
while(rs.next()){
String a,b;
a=rs.getString("id");
b=rs.getString("zy");
out.print("<zy>");
out.print("<zyname>"+b+"</zyname>");
out.print("<zyvalue>"+a+"</zyvalue>");
out.print("</zy>");
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
out.print("</response>");
out.flush();
out.close();
}

/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}

}


百度到说问题是因为response.setContentType("text/xml;charset=utf-8");这个的问题 可是写上了还是不对 求解0.0
...全文
2290 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
Go 旅城通票 2017-06-09
  • 打赏
  • 举报
回复
ZyServlet这个url地址报错执行到catch输出其他不符合xml内容的字符了吧,自己打印返回的内容看是什么 if(xmlHttp.readyState==4){ if(xmlHttp.status==200){ alert(xmlHttp.responseText) zys=xmlHttp.responseXML.getElementsByTagName("zy");
当作看不见 2017-06-09
  • 打赏
  • 举报
回复
无法在一null 原型上面读取getElementsByTagName 也就是你看一下报错的行,这一行的调用getElementsByTagName 方法的是一个null 值,你需要做一个判断,只有不是为null 的情况下,才使用函数getElementsByTagName.
珍惜最爱的你 2017-06-09
  • 打赏
  • 举报
回复
今天又系统的学了一下 已经会写了 谢谢

52,797

社区成员

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

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