简单的AJAX异步处理

1 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
2  <html xmlns="http://www.w3.org/1999/xhtml">
3  <head>
4  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
5  <title>无标题文档</title>
6 <script type="text/javascript">
7 var xmlHttp;
8 function createXMLHttpRequest()
9 {
10 if(window.ActiveXObject)//判断浏览器异步对象类型;
11 {
12 xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");//实例化异步对象;
13 }
14 else if(window.XMLHttpRequest)
15 {
16 xmlHttp=new XMLHttpRequest();
17 }
18 }
19 function startRequest()
20 {
21 createXMLHttpRequest();//调用异步对象;
22 xmlHttp.onreadystatechange=function()//响应服务器调用方法;
23 {
24 if(xmlHttp.readyState==4)//判断请求是否完成
25 {
26 var a=xmlHttp.responseText; //获取服务器返回的数据并赋给变量a
27 alert(a)
28 }
29 }
30 xmlHttp.open("GET","admin.php",true);//通过GET方法访问一个服务器端的URL链接脚本并以异步处理建立一个服务器请求
31 xmlHttp.send(null);//发送一个请求;
32 }
33 </script>
34 </head>
35
36 <body>
37 <input type="button" value="提交" onclick="startRequest()" />
38 </body>
39 </html>
40
41 admin.php
42 <?php
43 echo "my name is youyou"
44 ?>

 

总结:

1:判断浏览器并实例化XMLHttpRequest对象;

2:相应服务器执行的代码段;

3:建立通过open方法访问服务器脚本语言的请求;

4:通过send方法发送这个请求;

 

 

posted @ 2010-11-11 13:01  $雨$  阅读(406)  评论(0编辑  收藏  举报