利用ajax实现无刷新更新数据
show.html //客户端
<script language="JavaScript">
function GetResult() {
http_request = false;
//开始初始化XMLHttpRequest对象
if(window.XMLHttpRequest) { //Mozilla 浏览器
http_request = new XMLHttpRequest();
if (http_request.overrideMimeType) {//设置MiME类别
http_request.overrideMimeType('text/xml');
}
}
else if (window.ActiveXObject) { // IE浏览器
try {
http_request = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
http_request = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e) {}
}
}
if (!http_request) { // 异常,创建对象实例失败
window.alert("不能创建XMLHttpRequest对象实例.");
return false;
}
http_request.open("POST","rand.php",false);
http_request.send();
var strResult = http_request.responseText;
RemoveRow(); //删除以前的数据.
num1 = strResult; //字段num1的值
row1 = tb.insertRow();
cell1 = row1.insertCell();
cell1.innerText = num1;
}
function RemoveRow() {
//保留第一行表头,其余数据均删除.
var iRows = tb.rows.length;
for(var i=0;i<iRows-1;i++) {
tb.deleteRow(1);
}
}
function MyShow() {
//2秒自动刷新一次,2秒取得一次数据.
timer = window.setInterval("GetResult()",2000);
}
</script>
<body οnlοad="MyShow()">
<p>
</p>
<table width="47%" height="23"border="0" cellpadding="1" cellspacing="0" id="tb">
<tr>
<td>num1</td>
</tr>
</table>
get.php //服务端
<?
echo rand();
?>