日期:2014-05-17  浏览次数:21166 次

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();
        }
  &nbs