日期:2014-05-17  浏览次数:20794 次

ajax 表单验证 JSP
前台代码:
HTML code

<script type="text/javascript">
    function checkInDB() {
        with (loginForm) {
            var xmlhttp;
            if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
                xmlhttp = new XMLHttpRequest();
            } else {// code for IE6, IE5
                xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
            }
            xmlhttp.onreadystatechange = function() {
                if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
                    var s=xmlhttp.responseText;
                    alert(s);

                }
            }
            xmlhttp.open("POST", "UserLogin", true);
            xmlhttp.setRequestHeader("Content-type",
                    "application/x-www-form-urlencoded");
            xmlhttp.send("userType="+userType.value+"&userID=" + userID.value + "&userPassword="
                    + userPassword.value);
        }
    }
</script>




后台验证代码:
Java code

response.setContentType("text/html; charset=gb2312");
response.setHeader("Cache-Control", "no-cache");
PrintWriter out=response.getWriter();
out.write("UserNotFound!");
out.close();



alert(s)的结果为空,没有显示"UserNotFound!",这是为什么啊?!!!!

------解决方案--------------------
LZ这段代码可以正常显示UserNotFound!啊。先用firebug查查来回都传了些啥吧

------解决方案--------------------
看你代码肯定不会返回UserNotFound啦。
你都说了userType的值是“undefined”

那么程序根本不会执行
Java code

        if(userType.equals("reader")){
            readerLoginIn(request,response);
            return ;
        }
        if(userType.equals("manager")){
            managerLoginIn(request,response);
            return ;
        }