【发布时间】:2013-05-16 07:04:37
【问题描述】:
我想每五秒在我的页面中动态地重新加载一个 div。但在我的回复中,我得到了整个页面的内容......我怎样才能只得到指定 div 的内容?
<script>
function ajaxrefresh()
{
var xmlhttp;
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
//Here i want only the content for the div from the response
document.getElementById("myDiv").innerHTML=xmlhttp.responseText;
}
else
{
//alert("state: "+xmlhttp.readyState)
//alert("status: "+xmlhttp.status)
}
}
xmlhttp.open("GET","http://${localHostAddress}:8080/adress",true);
xmlhttp.send();
var t=setTimeout(ajaxrefresh,5000);
}
window.load = ajaxrefresh();
</script>
【问题讨论】:
-
如果数据不是个人或秘密的,例如银行余额,在服务器上创建只发回您想要的信息的软件可能比在浏览器端添加越来越多的代码更可取。将此软件绑定到不同的 URL。然后,您可以使用 ajax 仅从该特定 URL 请求该数据。优点:减少丑陋的黑客攻击,减少网络使用浪费,更好的可扩展性
标签: javascript html ajax