源码下载地址:http://download.csdn.net/source/782646
静态页面
- <!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>
- <title>Ajax获取数据过程</title>
- <script language="javascript" type="text/javascript">
- var xmlHttp;
- /*****创建XMLHttpRequest*****
- 如果是ie则执行第一个.
- ******************/
- function createXMLHttpRequest(){
- if(window.ActiveXObject)
- xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
- else if(window.XMLHttpRequest)
- xmlHttp=new XMLHttpRequest();
- }
- /*****执行异步调用*****
- open();多个重载:
- 参数一,GET或POST
- 参数二,url路径
- 参数三,异步标志,true即异步
- 参数四,五,用户名,密码
- onreadystatechange属性设置回调函数
- send()发送open()方法建立的请求.
- 有可选参数.可为空.
- ******************/
- function startRequest(){
- createXMLHttpRequest();
- xmlHttp.open("GET","AjaxServe.aspx?firstName=treebirthday=1981-03-21",true);
- xmlHttp.onreadystatechange=callback_state;//用函数名,不加括号.
- xmlHttp.send(null);
- }
- /*****回调函数*****
- xmlHttp.responseText;//取得返回数据
- readyState,对象的状态:
- 0,未初始化
- 1,正在装载
- 2,装载完毕
- 3,交互
- 4,完成
- status,服务器请求HTTP状态代码的数字代码
- ******************/
- function callback_state(){
- if(xmlHttp.readyState==4 && xmlHttp.status==200)
- document.getElementById("target").innerHTML+=xmlHttp.responseText;
- //document.write(xmlHttp.responseText);
- }
- </script>
- </head>
- <body>
- <input type="button" value="测试" onclick="startRequest()" />
- <br /><br />
- <div id="target">目标:<br /></div>
- </body>
- </html>
动态页面
- protected void Page_Load(object sender, EventArgs e)
- {
- if (Request.HttpMethod == "POST")
- {
- Response.Write("POST:" + Request["firstName"] + ",your birthday is" + Request["birthday"]);
- Response.End();
- }
- else if (Request.HttpMethod == "GET")
- {
- Response.Write("GET:" + Request["firstName"] + ",your birthday is" + Request["birthday"]);
- Response.Write("<br/>");
- //Response.Write("<script>alert('Hello');</script>");
- Response.End();
- }
- }