ASP+AJAX简单实例

<!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=gb2312" />
<title>兼容多浏览器的AJAX入门实例(超详细注释)</title>
<script type="text/javascript">
<!--

var xmlHttp
/*建立XMLHTTP对象调用MS的ActiveXObject方法,如果成功(IE浏览器)则使用MS ActiveX实例化创建一个XMLHTTP对象*/
//非IE则转用建立一个本地Javascript对象的XMLHttp对象 (此方法确保不同浏览器下对AJAX的支持)
function createXMLHttp(){
    if(window.ActiveXObject){
        xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
    else if(window.XMLHttpRequest){
        xmlHttp = new XMLHttpRequest();
    }
}

//建立主过程
function startXMLHttp(){
    createXMLHttp(); //建立xmlHttp 对象
    xmlHttp.open("get","server.asp",true); //建立一个新的http请求,传送方式 读取的页面 异步与否(如果为真则自动调用dodo函数)
    xmlHttp.send(); //发送
xmlHttp.onreadystatechange =doaction; //xmlHttp下的onreadystatechange方法 控制传送过程
}

function doaction(){
    if(xmlHttp.readystate==4){ // xmlHttp下的readystate方法 4表示传送完毕
        if(xmlHttp.status==200){ // xmlHttp的status方法读取状态(服务器HTTP状态码) 200对应OK 404对应Not Found(未找到)等
             document.getElementById("content").innerHTML=xmlHttp.responseText //xmlHttp的responseText方法 得到读取页数据
           }
}

}
-->
</script>
</head>

<body>
<span id="content"><!--读取后用来显示的区域--></span><br>
<input type="button" οnclick="javascript:startXMLHttp()" value="查询"/>
</body>
</html>

 

 

server.asp页面:

<table width="100%" border="1">
<tr>
    <td>AJAX处理结果。</td>
</tr>
</table>

<%
'-------------------------------------------
'//禁止缓存该页 让AJAX读取该页始终为最新而非过期缓存页
Response.Expires = 0
Response.Expiresabsolute = Now() - 1
Response.AddHeader "pragma","no-cache"
Response.AddHeader "cache-control","private"
Response.CacheControl = "no-cache"
'-------------------------------------------
response.Charset="GB2312" '//数据返回的编码类型 显示中文数据必须
'-------------------------------------------
response.Write(now())'//得到当前时间

'在这里还可以进行一大堆数据库操作。
%>

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值