为了账号安全,请及时绑定邮箱和手机立即绑定

提示这样的错误:Uncaught TypeError: Cannot read property 'getElementsByTagName' of null

/ 猿问

提示这样的错误:Uncaught TypeError: Cannot read property 'getElementsByTagName' of null

慕粉175552829 2019-03-17 15:00:40

alert明明有输出却还是显示这样的错误,ballball各位大佬教教我!!!

<div class="form-group">

<div class="label">

<label>工号:</label>

</div>

<div class="field">

<input type="text" style="width:25%; float:left;"

class="input w50" value="" name="userid" id ="userid" onblur="validate()"

data-validate="required:请输入工号" />

<div id="usermsg"></div>

</div>

</div>

<script type="text/javascript">

var req;

function validate() {

var idField = document.getElementById("userid");

var url = "${pageContext.request.contextPath}/page/user/validate.jsp?id="

+ escape(idField.value);

// alert(url);

if (window.XMLHttpRequest) {

req = new XMLHttpRequest();

} else if (window.ActiveXObject) {

req = new ActiveXObject("Microsoft.XMLHTTP");

}

req.open("GET", url, true);

req.onreadystatechange = callback;

req.send(null);

}


function callback() {

if (req.readyState == 4) {

if (req.status == 200) {

alert(req.responseText);

var msg = req.responseXML.getElementsByTagName("msg")[0];

// alert(msg);


setMsg(msg.childNodes[0].nodeValue);

}

}

}


function setMsg(msg) {

// alert(msg);

mdiv = document.getElementById("usermsg");

if (msg == "invalid") {

mdiv.innerHTML = "<font color='red'>username exists</font>";

} else {

mdiv.innerHTML = "<font color='green'>congratulations! you can use this username!</font>";

}

}

</script>

validate.jsp

<body>

<%

response.setContentType("text/xml");

response.setHeader("Cache-Control", "no-store"); //HTTP1.1

response.setHeader("Pragma", "no-cache"); //HTTP1.0

response.setDateHeader("Expires", 0); //prevents catching at proxy server

response.setHeader("Cache-Control", "no-cache");


System.out.println(request.getParameter("id"));

String userid = request.getParameter("id");

System.out.println(userid);


User u = new User();


boolean userExist = u.CheckUserid(userid);

System.out.println(userExist);

if (userExist) {

response.getWriter().write("<msg>invalid</msg>");

} else {

response.getWriter().write("<msg>valid</msg>");

}

%>

</body>


查看完整描述

1 回答

?
pardon110

插入文档节点用appendChild方法,不要将标签元素节点当作文档流用write方法输出。直接write输出,浏览器会认为你所找的并非是同一文档document对象,后果就是 通过getElementsByTag('msg')找不到msg标签

查看完整回答
反对 回复 2019-03-31

添加回答

回复

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信