ajaxUpdate


<html>
<head>
<title> Ajax实例</title>
<script language = "javascript" type ="text/javascript">
//定义一个变量,存放xmlhttprequest对象
    var xmlHttp;
//定义一个用于创建xmlhttprequest对象的函数
function createXMLHttpRequest() {
    if (window.ActiveXObject) {
        xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
    else if (window.XMLHttpRequest) {
    xmlHttp = new XMLHttpRequest();
    }
}

//响应HTTP请求状态变化的函数
function httpStateChange() {
//判断异步调用是否完成
    if (xmlHttp.readyState == 4) {
    //判断异步调用是否成功,如果成功则开始局部更新数据
        if (xmlHttp.status == 200 || xmlHttp.status == 0) {
            //查找节点
            var node = document.getElementById("myDiv");
            //更新数据
            node.firstChild.nodeValue = xmlHttp.responseText;
        }
        else {
        //如果异步未成功
            alert("异步调用出错\n返回的HTTP状态为:" + xmlHttp.status + "\n返回的Http状态信息为:" + xmlHttp.statusText);
        }
    }

}

//异步调用服务器端数据
function getData() {
//创建xmlhttprequest对象
    createXMLHttpRequest();
    if (xmlHttp != null) {
    //创建http请求
        xmlHttp.open("get", "ajax.txt", true);
        //设置响应htttp请求状态变化的函数
        xmlHttp.onreadystatechange = httpStateChange;
        //发生请求
        xmlHttp.send(null);
    }
}
</script>
</head>
<body>
<div id = "myDiv" >原数据</div>
<input  type ="button" value ="更新数据"  onclick = "getData()"/>
</body>
</html>

转载于:https://www.cnblogs.com/cityboy42/archive/2010/04/06/1705171.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值