xmlHttp.readyState=-2是几个意思?

TatsuyaLMinami 2017-05-01 09:11:21
之前那和帖子问题是js return false后继续执行action,我找了下原因,把 readyState错误码打印出来一看是-2.。。不知道怎么办了,是if else 哪里出错了么?我是新手,希望大家帮帮忙啊!
js:
function checkuser() {
var username = f1.username.value;
var userpassword = f1.userpassword.value;
var xmlHttp;
if(window.XMLHttpRequest) {
xmlHttp = new XMLHttpRequest();
} else if (window.ActiveXObject) {
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlHttp.open("GET", "com/servlet/ValidateUsernameServlet?username=" + username, true);
xmlHttp.send(null);
xmlHttp.onreadystatechange =function() {
if (xmlHttp.readyState == 4 && xmlHttp.status == 200) {
if (xmlHttp.responseText == "true") {
document.getElementById("usernameid").innerHTML = "√";
return true;
}else {
document.getElementById("usernameid").innerHTML = "<font color='red'>"+"昵称或密码错误!"+ "</font>";
f1.username.focus();
return false;
}
} else {
alert("错误码:" + xmlHttp.readyState);
f1.username.focus();
return false;
}
}
}
jsp:
<form
id="f1" style=" position:fixed;top:90px;left:415.5px;width:500px;height:250px;"
class="navbar-form navbar-left" role="form" action="<%=basePath %>login" method="put" onsubmit="return checkuser()" >
<!-- <DIV style="background: rgb(255, 255, 255); margin: -100px auto auto; border: 1px solid rgb(231, 231, 231); border-image: none; width: 400px; height: 200px; text-align: center;"> -->
<div class="input-group1">
<span class="input-group-addon">昵称:</span>
<input id="username" name="username" type="text" class="input"
placeholder="1-10个汉字、字母、数字组合" size="31" aria-describedby="basic-addon1" onblur="checkusername()" >
<span id="usernameid"></span>
</div>
<div class="input-group2" id="userpasswordiv">
<span class="input-group-addon">密码:</span>
<input name="userpassword" type="password" class="input"
placeholder="6-12位(必须包含数字及字母)" size="31" aria-describedby="basic-addon1" onblur="checkuserpassword()" >
<span id="userpasswordid"></span>
</div>
<button type="submit" class="btn btn-default" style="position: fixed;top:255px;left:465.5px;width:200px;height:35px; ">登录</button>
</form>
...全文
541 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
TatsuyaLMinami 2017-05-05
  • 打赏
  • 举报
回复
引用 1 楼 pany1209 的回复:
怎么不直接jquery的ajax请求???
我把js改成这样了,现在还是不行,弹出3之后继续提交,能在看看问题出在那哪里么,是不是逻辑有问题? function checkusername() { var username = f1.username.value; if (!(username==null) && !(username=="")) { document.getElementById("usernameid").innerHTML = ""; return true; } } function checkuserpassword() { var userpassword=f1.userpassword.value; if(!(userpassword==null)&& !(userpassword=="")){ document.getElementById("userpasswordid").innerHTML =""; return true; } } function checkuser() { var username = f1.username.value; var userpassword = f1.userpassword.value; if (username==null||username=="") { document.getElementById("usernameid").innerHTML = "<font color='red'>"+"昵称不能为空!"+ "</font>"; f1.username.focus(); return false; } else { document.getElementById("usernameid").innerHTML = ""; f1.userpassword.focus(); if(userpassword==null||userpassword==""){ document.getElementById("userpasswordid").innerHTML ="<font color='red'>" +"密码不能为空!"+ "</font>"; f1.userpassword.focus(); return false; }else { document.getElementById("userpasswordid").innerHTML = ""; $.ajax({ url:'com/servlet/ValidateUsernameServlet', data:{username:username,userpassword:userpassword}, type:'post', success:function(str) { if(str =="true" ){ document.getElementById("usernameid").innerHTML = "√"; alert("2"); return true; }else{ alert("3"); document.getElementById("usernameid").innerHTML = "<font color='red'>"+"昵称或密码错误!"+ "</font>"; return false; } }, error : function() { alert("未知错误!"); return false; }, }); } } }
李德胜1995 2017-05-04
  • 打赏
  • 举报
回复
怎么不直接jquery的ajax请求???

81,095

社区成员

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

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