jquery的ajax

jsp:
         var xmlHttp = $.post("elecUserAction_checkUser.do",{"logonName":object.value},function(data,textStatus){
                        //alert("data:"+data);
                        //alert("textStatus:"+textStatus);
                        //alert("xmlHttp.responseText:"+xmlHttp.responseText);
                        //alert("xmlHttp.readyState:"+xmlHttp.readyState);
                        //alert("xmlHttp.status:"+xmlHttp.status);
                        if(data==1){
                            document.getElementById("check").innerHTML = "<font color='red'>当前登录名已存在</font>"
                            object.focus();
                        }
                        else{
                            document.getElementById("check").innerHTML = "<font color='green'>当前登录名可以使用</font>"
                        }
                     });
       //或者
       $.ajax({
            type: "POST",
            async:true,  // 设置异步方式
            cache:false, // 保证ajax数据的实时性,不从缓存中读取数据,数据是最新的数据
            url: "elecUserAction_checkUser.do",
            data:"logonName="+logonName,

            success:function(data){
                if(data==1){
                                //alert("当前登录名已经存在");
                                document.getElementById("check").innerHTML = "<font color='red'>当前登录名已存在</font>";
                                //object.value = "";
                                object.focus();
                            }
                            else{
                                document.getElementById("check").innerHTML = "<font color='green'>当前登录名可用</font>";
                            }
            }

       });
      
action类:
方案一:使用struts2的ajax
         1:导入struts2-json-plugin-2.3.3.jar
         2:在修改struts.xml的extends为json-default(因为json-default继承了struts-default)
            添加:
             <result name="checkUser" type="json">
                 <!-- 使用root参数指定从服务器端返回到客户端的文本内容 -->
                 <param name="root">message</param>
             </result>
         3: 在模型驱动中添加message的属性
         4:添加方法
            public String checkUser(){
                            //获取登录名
                            String logonName = elecUser.getLogonName();
                            /**
                             * 以登录名作为查询条件,获取用户信息列表
                             *   如果列表的值不为null,说明登录名在数据库中存在记录,返回flag=1
                                 如果列表的值为null,说明登录名在数据库中不存在记录,返回flag=2
                             */
                            String flag = elecUserService.findUserByLogonName(logonName);
                            elecUser.setMessage(flag);
                           
                            return "checkUser";
                        }
方案二:ajax的基本调用
         1:添加方法
            public String checkUser(){
                            try {
                                //处理编码格式
                                request.setCharacterEncoding("UTF-8");
                                response.setContentType("text/html;charset=utf-8");
                                //获取登录名
                                String logonName = elecUser.getLogonName();
                                /**
                                 * 以登录名作为查询条件,获取用户信息列表
                                 *   如果列表的值不为null,说明登录名在数据库中存在记录,返回flag=1
                                     如果列表的值为null,说明登录名在数据库中不存在记录,返回flag=2
                                 */
                                String flag = elecUserService.findUserByLogonName(logonName);
                                PrintWriter out = response.getWriter();
                                out.println(flag);
                            } catch (IOException e) {
                                e.printStackTrace();
                            }
                            return null;
                        }

你可能感兴趣的:(jquery的ajax)