一个AJAX局部刷新的例子

本文介绍了一个使用AJAX实现局部刷新的示例,通过定时请求后台数据并更新页面部分内容,展示了基本的AJAX交互原理及其实现方式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一个AJAX局部刷新的例子:

前台页面:

 

<%@LANGUAGE="VBSCRIPT" CODEPAGE="65001"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>AJAX局部刷新</title>
<script type="text/javascript">
<!--

//建立XMLHttpRequest对象
var xmlhttp;
try{
    xmlhttp
= new ActiveXObject('Msxml2.XMLHTTP');
}
catch(e){
    
try{
        xmlhttp
= new ActiveXObject('Microsoft.XMLHTTP');
    }
catch(e){
        
try{
            xmlhttp
= new XMLHttpRequest();
        }
catch(e){}
    }
}

function getPart(url){
    xmlhttp.open(
"get",url,true);
    xmlhttp.onreadystatechange 
= function(){
        
if(xmlhttp.readyState == 4)
        {
            
if(xmlhttp.status == 200)
            {
                
if(xmlhttp.responseText!=""){
                    document.getElementById(
"partdiv").innerHTML = unescape(xmlhttp.responseText);        
                }
            }
            
else{
                document.getElementById(
"partdiv").innerHTML = "数据载入出错";
            }
        }
    }
    xmlhttp.setRequestHeader(
"If-Modified-Since","0");
    xmlhttp.send(
null);
}
setInterval(
"getPart('getPart.asp')",1000)
//-->
</script>
</head>

<body>
<div id="partdiv"></div><!--局部刷新数据的容器-->
</body>
</html>

 

后台页面:[getPart.asp]

 

<%@LANGUAGE="VBSCRIPT" CODEPAGE="65001"%>
<!--#include file="conn.asp"-->
<%
    
dim rs
    
dim sql

    
    
Set rs = Server.CreateObject("ADODB.Recordset")
    sql 
= "select * from king_test"
    rs.open sql,conn,
1,1
    
if not (rs.bof and rs.eof) then
    Response.Write(
"<table>")
    Response.Write(escape(
"<tr><td>ID</td><td>关键字</td></tr>"))
    
do while not rs.eof
%>
<tr><td><%Response.Write(rs("id"))%></td><td><%Response.Write(escape(rs("keyword")))%></td></tr>
<%
    rs.movenext
    
loop
    Response.Write(
"</table>")
    
end if
    rs.close
    
set rs = nothing
    conn.close
    
Set conn = nothing
%>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值