一个ASP+AJAX的简单实例,注释相当详细,可以当作AJAX入门(或许和网上一些程序相似)
一、建立主页文档,命名为:ajax.html
<script language="javascript" src="/js.js"></script>
<body style="text-align:center;">
<br /><br />
<span id="content">显示系统时间</span><br /><br />
<input type="button" value="AJAX获取"/>
</body>
<body style="text-align:center;">
<br /><br />
<span id="content">显示系统时间</span><br /><br />
<input type="button" value="AJAX获取"/>
</body>
二、建立JavaScript 文档,命名为:js.js
var xmlHttp
function CreateXMLHttpRequest()
{// Initialize Mozilla XMLHttpRequest object
if(window.XMLHttpRequest)
{ xmlHttp = new XMLHttpRequest()}
// Initialize for IE/Windows ActiveX version
else if(window.ActiveXObject)
{
function CreateXMLHttpRequest()
{// Initialize Mozilla XMLHttpRequest object
if(window.XMLHttpRequest)
{ xmlHttp = new XMLHttpRequest()}
// Initialize for IE/Windows ActiveX version
else if(window.ActiveXObject)
{
try{xmlHttp = new ActiveXObject("Msxml2.XMLHTTP.3.0");}
catch(e){
try{xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");}
catch(e){alert( "对不起,您的浏览器不支持XMLHttpRequest对象!");}
}
}
return xmlHttp;
}
//让ajax 支持多钟浏览器
function getObject(objectId)
{
if(document.getElementById && document.getElementById(objectId))
{// W3C DOM
return document.getElementById(objectId);}
else if(document.all && document.all(objectId))
{// MSIE 4 DOM
return document.all(objectId);}
else if(document.layers && document.layers[objectId])
{ // NN 4 DOM -----note: this won't find nested layers
return document.layers[objectId];}
else{
return false;}
}
//ajax异步读取
function startXMLHttp()
{
CreateXMLHttpRequest(); //建立xmlHttp 对象
xmlHttp.onreadystatechange = getcontent ; //xmlHttp下的onreadystatechange方法 控制传送过程
xmlHttp.open("get","ajax.asp",true); //传送方式 读取的页面 异步与否
xmlHttp.send(); //发送
}
function getcontent()
{
var objectType = "content"
if (xmlHttp.readyState == 4 && xmlHttp.status == 200) // xmlHttp下的readystate方法 4表示传送完毕
{ // xmlHttp的status方法读取状态(服务器HTTP状态码) 200对应OK 404对应Not Found(未找到)等
getObject(objectType).innerHTML = xmlHttp.responseText; //xmlHttp的responseText方法 得到读取页数据
//document.getElementById("content").innerHTML = xmlHttp.responseText;
}
}
catch(e){
try{xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");}
catch(e){alert( "对不起,您的浏览器不支持XMLHttpRequest对象!");}
}
}
return xmlHttp;
}
//让ajax 支持多钟浏览器
function getObject(objectId)
{
if(document.getElementById && document.getElementById(objectId))
{// W3C DOM
return document.getElementById(objectId);}
else if(document.all && document.all(objectId))
{// MSIE 4 DOM
return document.all(objectId);}
else if(document.layers && document.layers[objectId])
{ // NN 4 DOM -----note: this won't find nested layers
return document.layers[objectId];}
else{
return false;}
}
//ajax异步读取
function startXMLHttp()
{
CreateXMLHttpRequest(); //建立xmlHttp 对象
xmlHttp.onreadystatechange = getcontent ; //xmlHttp下的onreadystatechange方法 控制传送过程
xmlHttp.open("get","ajax.asp",true); //传送方式 读取的页面 异步与否
xmlHttp.send(); //发送
}
function getcontent()
{
var objectType = "content"
if (xmlHttp.readyState == 4 && xmlHttp.status == 200) // xmlHttp下的readystate方法 4表示传送完毕
{ // xmlHttp的status方法读取状态(服务器HTTP状态码) 200对应OK 404对应Not Found(未找到)等
getObject(objectType).innerHTML = xmlHttp.responseText; //xmlHttp的responseText方法 得到读取页数据
//document.getElementById("content").innerHTML = xmlHttp.responseText;
}
}
三、建立信息页面,命名为:ajax.asp
<%
'-------------------------------------------
'//禁止缓存该页 让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())'//打印时间
'-------------------------------------------
'//禁止缓存该页 让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())'//打印时间
%>
转载于:https://blog.51cto.com/shind/109319