当前页面: 开发资料首页 → J2EE 专题 → 怎么实现局部无刷新取数据?
怎么实现局部无刷新取数据?
摘要: 怎么实现局部无刷新取数据?
我在一个页面中用层显示数据库中的数据,但数据库中的数据实时更新(10S一次),要求层中的数据为最新更新数据,而且不能刷新整个页面,只刷新层.baidu了一下,好象xmlhttp技术可以,但不会用.
哪位达人给个解决方案吧,最好附个简短的例子.
1.jsp
<%=new Date()%>
2.jsp
<script>
function createHttpRequest() {
if (window.ActiveXObject) {
return new ActiveXObject('Microsoft.XMLHTTP');
} else if (window.XMLHttpRequest) {
return new XMLHttpRequest();
} else {
throw new Error(9999, '无法创建HttpRequest!');
}
}
var httpRequest = createHttpRequest();
httpRequest.open('GET','1.jsp',false);
httpRequest.send(null);
if(httpRequest.status==200){
document.getElementById("dt").innerHTML=httpRequest.responseText;
}
</script>
AJAX
<script>
function updateDiv() {
var tar = document.getElementById("div1");
tar.innerHTML = ajaxMethod.value; //ajaxMethod为xmlhttp代码,可以用现成的ajax框架
settimeout(10000, "updateDiv()");
}
</script>