原生js实现ajax回传text

$("#userName").blur(function() {
            var username = $("#userName").val();
            var xh = getXMLHttp();
            var url ='<%=path%>/servlet/DoLogin?oper=ajaxUC&userName='+username;
            xh.open("GET", url, true);
            xh.onreadystatechange = function() { 
                if((xh.readyState == 4)){
                        if ( xh.status == 200) { 
                            var info=xh.responseText;
                            if(info=="1"){
                            $("#loginNameSpan").text("用户可登录");
                            }else{
                            $("#loginNameSpan").text("用户不存在");
                            }
                        } else{
                            $("#loginNameSpan").text("用户不存在");
                        }
                }else{
                            $("#loginNameSpan").text("用户不存在");
                }
            };
            xh.send(null);
        });
    
        //获取XMLHttpRequest对象, 并兼容浏览器
        function getXMLHttp() {
            if (window.XMLHttpRequest) {
                return new XMLHttpRequest();
            } else if (window.ActioveXObject) {
                return new ActiveXObject("Microsoft.XMLHTTP");
            } else {
                alert("您的浏览器不支持XMLHTTP");
                return null;
            }
        };
try {
            String userName = request.getParameter("userName");
            String userPassword = request.getParameter("userPassword");
            Smbms_user su=new Smbms_user();
            su.setUserCode(userName);
            su.setUserPassword(userPassword);
            UserService us=new UserServiceImpl();
            String info="";
            
            if(us.dologin(su)!=null){
                info="1";
            }else{
                info="0";
            }
            PrintWriter writer = response.getWriter();
            writer.write(info);
        } catch (IOException e) {
            
            e.printStackTrace();
        }        

 

posted @ 2016-05-16 10:12  胡一生  阅读(288)  评论(0编辑  收藏  举报