当前位置: 代码迷 >> ASP >> ASP.NET应用AJAX
  详细解决方案

ASP.NET应用AJAX

热度:296   发布时间:2012-10-14 14:55:08.0
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>

  相关解决方案