纯Javascript代码发送AJAX请求的代码

<script language="javascript" type="text/javascript">
        function btnClick() {
            var xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
            if (!xmlhttp) {
                alert("创建xmlhttp对象失败!");
                return false;
            }
            xmlhttp.open("POST", "GetData1.ashx?ts=" + new Date(), false); //准备向服务器的GetData1.ashx发出Post请求
            //XMLHTTP默认(也推荐)不是同步请求的,也就是open方法并不像WebClient的DownloadString那样把服务器返回的数据拿到才返回,是异步的,因为需要监听onreadystatechange事件
            xmlhttp.onreadystatechange = function () {
                if (xmlhttp.readystate = 4) {//服务器完成
                    if (xmlhttp.state = 200) {//如果装态码为200则是成功
                        alert(xmlhttp.responseText); //responseText为服务器返回的文本,弹出返回内容
                    }
                    else {
                        alert("AJAX服务器返回错误");
                    }
                }
            }
            xmlhttp.send();//这时才开始发送请求
        }   
    </script>
posted @ 2011-03-10 10:06  ycmoon  阅读(410)  评论(0编辑  收藏  举报
QQ:817647 MSN:kenny@msn.cn EMail:ycmoon@qq.com