您的位置:首页 > 编程语言 > ASP

一个AJAX局部刷新 asp

2011-05-25 15:07 134 查看
<html>
<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]
<!--#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
%>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息