[jQuery]Ajax异步检测注册用户名是否已存在或重复
时间:2016-10-30 来源:酷播 点击:
[jQuery]Ajax异步检测注册用户名是否已存在或重复
- function validate(field) {
- if (trim(field.value).length != 0) {
- //CuPlayer.com提示:创建Ajax核心对象XMLHttpRequest
- createXMLHttpRequest();
- var url = "user_validate.jsp?userId=" + trim(field.value) + "&time=" + new Date().getTime();
- //设置请求方式为GET,设置请求的URL,设置为异步提交
- xmlHttp.open("GET", url, true);
- //将方法地址复制给onreadystatechange属性
- //类似于电话号码
- xmlHttp.onreadystatechange=callback;
- //将设置信息发送到Ajax引擎
- xmlHttp.send(null);
- } else {
- document.getElementById("spanUserId").innerHTML = "";
- }
- }
- function callback() {
- //alert(xmlHttp.readyState);
- //Ajax引擎状态为成功
- if (xmlHttp.readyState == 4) {
- //HTTP协议状态为成功
- if (xmlHttp.status == 200) {
- if (trim(xmlHttp.responseText) != "") {
- //alert(xmlHttp.responseText);
- document.getElementById("spanUserId").innerHTML = "<font color='red'>" + xmlHttp.responseText + "</font>";
- }else {
- document.getElementById("spanUserId").innerHTML = "";
- }
- }else {
- alert("CuPlayer.com提示:请求失败,错误码=" + xmlHttp.status);
- }
- }
- }
user_validate.jsp页面接收用户Id并根据Id查询是否已存在,如果存在返回,不存在什么也不返回。
- 1 <%
- 2 String userId = request.getParameter("userId");
- 3 if(UserManager.getInstance().findUserById(userId) != null) {
- 4 out.println("CuPlayer.com提示:用户代码已经存在");
- 5 }
- 6 %>