我想動態地每五秒鐘在我的頁面上重新加載一個div。但在我的迴應中,我得到了整個頁面的內容......我怎樣才能得到指定div的內容?只用AJAX刷新一個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>
響應提供的數據是不是個人或祕密的,像銀行存款餘額,在服務器上創建軟件只發送你想要的信息可能比向瀏覽器端添加越來越多的代碼更可取。將此軟件綁定到其他網址。然後,您可以使用ajax僅請求該特定URL中的那部分數據。優點:更少的醜陋黑客,更少的網絡使用浪費,更好的可擴展性 – Paul