局部刷新一个div

来源:互联网 发布:药智数据app 编辑:程序博客网 时间:2024/04/30 01:19

最近学习ajax,做了个小例子:

<script type="text/javascript">var xmlhttp;function startrefresh(){xmlhttp=new XMLHttpRequest();xmlhttp.open("POST",http://localhost:8080/myApp/test.jsp,true);xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");xmlhttp.send("name=wk");xmlhttp1.onreadystatechange = function(){if(xmlhttp.readyState == 4)if(xmlhttp.status == 200)document.getElementById("mydvi").innerHTML=xmlhttp.responseText;}}</script>


其中xmlhttp对象是负责客户端和服务器交互的,如果要传递参数要用setRequestHeader方法先,然后用send传入参数

还有,取得xmlhttp.responseText必需经过先前的验证,否则会提示:“完成该操作所需的数据还不可使用”;

如果让这个div自动刷新的话,可以用setInterval('startrefresh()',5000),这个函数的作用是每隔5秒自动执行一次startrefresh方法,还有一种方法是setTimeout('startrefresh()',5000),但是这个方法只会执行一次。

原创粉丝点击