ASP.NET使用AJAX

GET方式:(获取参数使用Request.QueryString)

    <script type="text/javascript">
        function createXMLHttpRequest() {
            if (window.ActiveXObject)//判断是不是IE浏览器
                return new ActiveXObject("Microsoft.XMLHTTP");
            else
                return new XMLHttpRequest();
        }
        var xmlHttpRequest;
        function CheckName() {
            var name = document.getElementById("userName").value;
            var url = "Default2.aspx?userName=" + name;
            //创建XMLHttpRequest对象
            xmlHttpRequest = createXMLHttpRequest();
            //设置回调函数
            xmlHttpRequest.onreadystatechange = ReadyDo;
            //初始化XMLHttpRequest对象
            xmlHttpRequest.open("GET", url, true);
            //发送请求
            xmlHttpRequest.send();
        }
        function ReadyDo() {
            if (xmlHttpRequest.readyState == 4 && xmlHttpRequest.status == 200) {
                var result = xmlHttpRequest.responseText;
                if (result == "true") {
                    document.getElementById("msg").style.display = "none";
                 }
                else {
                    document.getElementById("msg").style.display = "inline";
                }
            }
        }
    </script>

 

POST方式:(获取参数方式Request.Form)

 <script type="text/javascript">
        function createXMLHttpRequest() {
            if (window.ActiveXObject)
                return new ActiveXObject("Microsoft.XMLHTTP");
            else
                return new XMLHttpRequest();
        }
        var xmlHttpRequest;
        function Login() {
            var userName = document.getElementById("<%= this.txtLoginId.ClientID %>").value;
            var password = document.getElementById("<%= this.txtPwd.ClientID %>").value;
            //创建XMLHttpRequest对象
            xmlHttpRequest = createXMLHttpRequest();
            //设置回调函数
            xmlHttpRequest.onreadystatechange = ReadyDo;
            //初始化XMLHttpRequest对象
            xmlHttpRequest.open("POST", "Default2.aspx", true);
            //如果是post提交要设置请求头部
            xmlHttpRequest.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");

            //发送请求
            var data = "userName=" + escape(userName) + "&password=" + escape(password);
            xmlHttpRequest.send(data);         }         function ReadyDo() {             if (xmlHttpRequest.readyState == 4 && xmlHttpRequest.status == 200) {                 var result = xmlHttpRequest.responseText;                 if (result == "true") {                     alert("登录成功");                  }                 else {                     alert("登录失败");                 }             }         }     </script>

相关文章
相关标签/搜索