当前位置: 代码迷 >> J2EE >> ajax有关问题。跪求
  详细解决方案

ajax有关问题。跪求

热度:154   发布时间:2016-04-19 22:43:13.0
ajax问题。跪求
js:<script type="text/javascript">
        var xmlHttp;//Ajax核心对象名称
        var flag;//定义标志位
         
        function createXMLHttp(){//创建XMLHttpRequest核心对象
            if(window.XMLHttpRequest){//判断浏览器类型
                xmlHttp = new XMLHttpRequest();
            }else{
                xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
            }
        }
         
        function checkUserId(Aname){
            createXMLHttp();
            //设置一个请求,通过地址重写的方式将userId传递到JSP中       
      xmlHttp.open("POST", "CheckRegAction?Aname=" + Aname,true);
      xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
            //设置请求完成后处理的回调函数
            xmlHttp.onreadystatechange = checkUserIdCallback;
            xmlHttp.send(null);
            document.getElementById("msg").innerHTML = "正在验证...";
        }
         
        function checkUserIdCallback(){//定义回调函数
            if(xmlHttp.readyState == 4){//数据返回完毕
                if(xmlHttp.status == 200){//HTTP操作正常
                    var text = xmlHttp.responseText;//接收返回内容
                    if(text == "true"){//这里接收pw回来的数据
                        flag = false;//找到了,无法提交注册表单
                        document.getElementById("msg").innerHTML = "用户ID已存在";
                    }else{
                        flag = true;//没找到此用户,可以提交注册表单
                        document.getElementById("msg").innerHTML = "用户ID可添加";
                    }
                }
            }
        }
         
        function checkForm(){
            return flag;
        }
    </script>

servlet:
 public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletExceptionIOException {
 
        this.doPost(request, response);
    }
 
   
    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
 
      request.setCharacterEncoding("GBK");
     response.setContentType("text/html");
       Connection conn = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        PrintWriter pw = response.getWriter();
        String Aname = request.getParameter("Aname");
       try {
            Class.forName(DBDRIVER);
conn = DriverManager.getConnection(DBURL, DBUSER, DBPSW);
             
  相关解决方案