XMLHTTP与客户端无刷新更新数据

[ 2006-05-13 00:11:22 | 作者: admin ]
字号: | |
注意:此方法在Firefox下无法使用!

一、XMLHTTP的基本使用方法

GET方式

<script>
var HttpRequest = new ActiveXObject("Microsoft.XMLHTTP");
HttpRequest.open("GET", "http://www.google.com", false);
HttpRequest.send();
alert(HttpRequest.responseText);
</script>


POST方式

<script>
var HttpRequest = new ActiveXObject("Microsoft.XMLHTTP");
HttpRequest.open("POST", "http://www.google.com", false);
HttpRequest.send("user=wo");
alert(HttpRequest.responseText);
</script>

稍微复杂些的代码

<script>
var HttpRequest = new ActiveXObject("Microsoft.XMLHTTP");
HttpRequest.open("POST", "http://www.google.com", false);
HttpRequest.setRequestHeader("Content-Type", "text/xml;charset=utf-8");
HttpRequest.setRequestHeader("HOST", "www.google.com");
HttpRequest.setRequestHeader("user-agent", "Mozilla/4.0");
HttpRequest.send("user=wo");

switch(HttpRequest.Status)
{
        case 200:
               alert(HttpRequest.responseText);
               break;
        default:
               alert("状态:" + HttpRequest.Status);
}

</script>


二、客户端自动刷新

<script>
function RefreshMe()
{
        var HttpRequest = new ActiveXObject("Microsoft.XMLHTTP");
       

        // 2秒钟取得一次数据
        timer = window.setInterval("RefreshMe()", 2000);
}
</script>

<body onload="RefreshMe()">
评论Feed 评论Feed: http://blog.xg98.com/feed.asp?q=comment&id=567

这篇日志没有评论。

此日志不可发表评论。