美文网首页
Ajax异步验证用户名是否已经存在

Ajax异步验证用户名是否已经存在

作者: code武 | 来源:发表于2017-12-20 14:44 被阅读0次

    //定义一个请求

    varxmlHttp;

    functioncreateXMLHttpRequest() {

    //表示当前浏览器不是ie,如ns,firefox

    if(window.XMLHttpRequest) {

    xmlHttp =newXMLHttpRequest();

    }elseif(window.ActiveXObject) {

    xmlHttp =newActiveXObject("Microsoft.XMLHTTP");

    }

    }

    //field为获取用户所填写的用户名

    functionvalidate(field) {

    //判断用户名是否为空

    if(trim(field.value).length != 0) {

    //创建Ajax核心对象XMLHttpRequest

    createXMLHttpRequest();

    //将获取用户名发送到另一个jsp中去验证

    varurl ="user_validate.jsp?userId="+ trim(field.value) +"&time="+newDate().getTime();

    //设置请求方式为GET,设置请求的URL,设置为异步提交,true为异步,false为同步

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

    //将方法地址复制给onreadystatechange属性

    //类似于电话号码

    xmlHttp.onreadystatechange=callback;

    //将设置信息发送到Ajax引擎

    xmlHttp.send(null);

    }else{

    document.getElementById("spanUserId").innerHTML ="";

    }

    }

    //发送请求之后,返回的状体

    functioncallback() {

    //alert(xmlHttp.readyState);

    //Ajax引擎状态为成功

    if(xmlHttp.readyState == 4) {

    //HTTP协议状态为成功

    if(xmlHttp.status == 200) {

    if(trim(xmlHttp.responseText) !="") {

    //设置请返回的消息信息

    document.getElementById("spanUserId").innerHTML =""+ xmlHttp.responseText +"</font>"

    }else{

    document.getElementById("spanUserId").innerHTML ="";

    }

    }else{

    alert("请求失败,错误码="+ xmlHttp.status);

    }

    }

    }

    <%

    //Thread.currentThread().sleep(5000);

    String userId = request.getParameter("userId");

    if(UserManager.getInstance().findUserById(userId) !=null) {

    out.println("用户代码已经存在");

    }

    %>

    相关文章

      网友评论

          本文标题:Ajax异步验证用户名是否已经存在

          本文链接:https://www.haomeiwen.com/subject/mwygwxtx.html