js代碼:
var xhr = getXhr();
function getXhr(){
var xhr = null;
try {
xhr = new XMLHttpRequest();
} catch (trymicrosoft) {
try {
xhr = new ActiveXObject("Msxml2.XMLHTTP");
} catch (othermicrosoft) {
try {
xhr = new ActiveXObject("Microsoft.XMLHTTP");
} catch (failed) {
xhr = false;
}
}
}
if (!xhr) {
alert("err Happend!");
}
return xhr;
}
function check() {
var user = document.getElementById("user").value;
var url = "RegisterServlet?id=" + user;
if (xhr.readyState == 4 && xhr.status == 200) {
var str = xhr.responseText;
if (str=="OK") {
document.getElementById("checkname").innerHTML="用戶名可用";
} else {
doucument.getElementById("checkname").innerHTML="用戶名重復";
}
}
xhr.open('get',url,true);
xhr.send(null);
}
前端代碼:
servlet代碼:
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
request.setCharacterEncoding("UTF-8");
response.setCharacterEncoding("utf-8");
PrintWriter out=response.getWriter();
String username=request.getParameter("id");
System.out.println(username);
if(username.equals("1")){
out.println("OK");
}else {
out.println("NO");
}
}
我測試用戶名輸入是否為1來判斷,從servlet傳值過來,但是在jsp裡面span標簽那顯示不出來信息,
測試顯示這個錯誤:
ReferenceError: doucument is not defined,報錯行數在js的if判斷innerhtml行
各位大大幫忙看看
doucument.getElementById("checkname").innerHTML = "用戶名重復";
是document,不是doucument,第3,4個字符反了,馬虎了