ajax基础代码

<script type="text/javascript">
    var req;
    function validate() {
        var idField = document.getElementById("userid");
        var url = "Validate.jsp?id=" + escape(idField.value);
        if(window.XMLHttpRequest) {
            req = new XMLHttpRequest();
        } else if (window.ActiveXObject) {
            req = new ActiveXObject("Microsoft.XMLHTTP");
        }
        req.open("GET", url, true);
        req.onreadystatechange = callback;
        req.send(null);
    }
    
    function callback() {
        if(req.readyState == 4) {
            if(req.status == 200) {
                //alert(req.responseText);
                var msg = req.responseXML.getElementsByTagName("msg")[0];
                //alert(msg);
                setMsg(msg.childNodes[0].nodeValue);
            }
        }
    }
    
    function setMsg(msg) {
        //alert(msg);
        mdiv = document.getElementById("usermsg");
        if(msg == "invalid") {
            mdiv.innerHTML = "<font color='red'>username exists</font>";
        } else {
            mdiv.innerHTML = "<font color='green'>congratulations! you can use this username!</font>";
        }
    }
    

</script>

 

posted @ 2013-05-18 11:42  LsPoPi  阅读(120)  评论(0编辑  收藏  举报