• 手写ajax(js)


    //声明XMLHttpRequest对象
    var xmlHttp;

    //检测用户名是否存在
    function CheckName(userName)
    {
        createXMLHTTP();//创建XMLHttpRequest对象
        var url="DisposeEvent.aspx?Name="+userName+"&Event=Check";
        xmlHttp.open("GET",url,true);
        xmlHttp.onreadystatechange=checkUserName;
        xmlHttp.send(null);
    }

    //生成xmlhttp对象
    function createXMLHTTP()
    {
        if(window.XMLHttpRequest)
        {
            xmlHttp=new XMLHttpRequest();//mozilla浏览器
        }
        else if(window.ActiveXObject)
        {
            try
            {
                xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");//IE老版本
            }
            catch(e)
            {}
            try
            {
                xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");//IE新版本
            }
            catch(e)
            {}
            if(!xmlHttp)
            {
                window.alert("不能创建XMLHttpRequest对象实例!");
                return false;
            }
        }
    }

    //执行检测用户名回调函数
    function checkUserName()
    {
        if(xmlHttp.readyState==4)//判断对象状态
        {
            if(xmlHttp.status==200)//信息成功返回,开始处理信息
            {
                if(xmlHttp.responseText=="true")
                {
                    document.getElementById("imgName").src="images/true.gif";
                    //让注册按钮失效
                    document.getElementById("btnReg").disabled=false;
                }
                else
                {
                    document.getElementById("imgName").src="images/false.gif";
                    document.getElementById("btnReg").disabled=true;                        
                }
            }
        }
    }

    //注册新用户
    function regUser()
    {
        if(document.getElementById("userName").value=="")
        {
            alert("对不起,用户名不为空");
            return false;
        }
        if(document.getElementById("userPwd").value=="")
        {
            alert("对不起,密码不为空");
            return false;
        }
        var url="DisposeEvent.aspx?Name="+document.getElementById("userName").value+"&Pwd="+document.getElementById("userPwd").value+"&Event=Reg";
        xmlHttp.open("GET",url,true);
        xmlHttp.onreadystatechange=regUserInfo;//设置接受。当发送返回后执行regUserInfo方法
        xmlHttp.send(null);
    }

    //注册用户回调函数
    function regUserInfo()
    {
        if(xmlHttp.readyState==4)//判断对象状态
        {
            if(xmlHttp.status==200)//信息成功返回,开始处理信息
            {
                if(xmlHttp.responseText=="true")//获取的Response.Write("true");里面的文本
                {
                    alert("恭喜,新用户注册成功!");
                    document.getElementById("userName").value="";
                    document.getElementById("userPwd").value="";               
                }
                else
                {
                    alert("对不起,注册失败!");
                    document.getElementById("userName").value="";
                    document.getElementById("userPwd").value="";                        
                }
            }
        }
    }

  • 相关阅读:
    撩妹技能 get,教你用 canvas 画一场流星雨
    git详细使用教程入门到精通(史上最全的git教程)
    Google Performance工具,你还不会用?Git走起。
    使用PIE.htc让万恶的IE内核浏览器IE678支持CSS3部分属性
    HTML编码规范
    Canvas制作的下雨动画
    Flex 布局教程:语法和实例
    CSS编码规范
    奇葩程序写的神一样的注释,被老板看见会不会开出呢?
    Vertical-Align你应该知道的一切
  • 原文地址:https://www.cnblogs.com/online/p/1067986.html
Copyright © 2020-2023  润新知