码迷,mamicode.com
首页 > Web开发 > 详细

Js_Ajax_用户名检测

时间:2016-11-25 00:34:34      阅读:187      评论:0      收藏:0      [点我收藏+]

标签:检测   this   alt   null   用户   back   etc   send   erro   

输入"root",OK;输入其它,Error

ajax.jsp

    var xhr;
    function createXhr() {
        if (window.XMLHttpRequest) {
            xhr = new XMLHttpRequest();
        } else {
            xhr = new ActiveXObject("Microsoft.XMLHTTP");
        }
    }
    
    function chk(elm) {
        var name = elm.value;
        createXhr();
        xhr.onreadystatechange = callback;
        xhr.open("get", "AjaxServlet?para="+name, true);
        xhr.send(null);
    }
    function callback() {
        if (xhr.readyState == 4) {
            if (xhr.status == 200) {
                var t = xhr.responseText;
                //文本 --> Json
                var json = eval("(" + t + ")");
                var elmCmt=document.getElementById("cmt")
                if(json.result){
                    elmCmt.innerText="OK";
                }else{
                    elmCmt.innerText="ERR";
                }
            }
        }
    }
<body>
    <input type="text" onblur="chk(this)">
    <span id="cmt" style="color:red"></span>
    <br>
    <input type="text">
</body>

AjaxServlet.java

    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
            response.setCharacterEncoding("utf-8");
            AhJson aj = new AhJson();
            String para=request.getParameter("para");

            JSONObject jo = new JSONObject();
            if("root".equals(para) ){
                jo.put("result", true);
            }else{
                jo.put("result", false);
            }
            
            response.getWriter().write(jo.toString());
    }

技术分享

Js_Ajax_用户名检测

标签:检测   this   alt   null   用户   back   etc   send   erro   

原文地址:http://www.cnblogs.com/AndyHoo/p/6100049.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!