ASP+AJAX简单实例

来源:互联网 发布:centos vsftpd 根目录 编辑:程序博客网 时间:2024/05/02 02:43

 

index.asp页面:

<%@LANGUAGE="VBSCRIPT" CODEPAGE="936"%>

<!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;

//根据不同浏览器创建不同的XMLHttpRequest对象

function createXMLHttp(){

  try{ // Firefox, Opera 8.0+, Safari

  xmlHttp=new XMLHttpRequest();

  }catch (e){ // Internet Explorer

  try{

  xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");

  }catch (e){

  try{

  xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");

  }catch (e){

  alert("您的浏览器不支持AJAX!");

  }

  }

  }

}


//建立主过程

function startXMLHttp(){

      createXMLHttp(); //建立xmlHttp 对象

  if( xmlHttp != undefined ){//简单检测xmlHttp是否获取为XMLHttpRequest对象,或者使用 typeof(xmlHttp) == "object" 来检测

  xmlHttp.open("get","server.asp",true);   //open() 方法需要三个参数。第一个参数定义发送请求所使用的方法(GET 还是 POST)。第二个参数规定服务器端脚本的 URL。第三个方法规定应当对请求进行异步地处理。

xmlHttp.send(null); //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" onclick="javascript:startXMLHttp()" value="查询"/>

</body>

</html>



 

 

server.asp页面:

<%@LANGUAGE="VBSCRIPT" CODEPAGE="936"%>

<%

'-------------------------------------------

'//禁止缓存该页 让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())'//得到当前时间


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

%>

 

 

 

 

http://ccp330.blog.163.com/blog/static/49300188200951283810314/

 

 

0 0
原创粉丝点击