一、XMLHttpRequest 对象的方法与属性
方 法 | 描 述 |
abort() | 停止当前请求 |
getAllResponseHeaders() | 把HTTP请求的所有响应首部作为键/值对返回 |
getResponseHeader("header") | 返回指定首部的串值 |
open("method", "url") | 建立对服务器的调用。method参数可以是GET、POST或PUT。url参数可以是相对URL或绝对URL。这个方法还包括3个可选的参数 |
send(content) | 向服务器发送请求 |
setRequestHeader("header", "value") | 把指定首部设置为所提供的值。在设置任何首部之前必须先调用open() |
属 性 | 描 述 |
onreadystatechange | 每个状态改变时都会触发这个事件处理器,通常会调用一个JavaScript函数 |
readyState | 请求的状态。有5个可取值:0 = 未初始化,1 = 正在加载,2 = 已加载,3 = 交互中,4 = 完成 |
responseText | 服务器的响应,表示为一个串 |
responseXML | 服务器的响应,表示为XML。这个对象可以解析为一个DOM对象 |
status | 服务器的HTTP状态码(200对应OK,404对应Not Found(未找到),等等) |
statusText | HTTP状态码的相应文本(OK或Not Found(未找到)等等) |
二、使用XMLHttpRequest 实现ajax效果
这里我们用客户端通过XMLHttpRequest 请求服务端获取当前系统时间,实现异步交互!
客户端myAjax.html代码
<
html
xmlns
="http://www.w3.org/1999/xhtml"
>
<
head
>
<
title
>
xmlhttprequest ajax demo
</
title
>
![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
<
script
type
="text/javascript"
language
="javascript"
>
var req; //定义变量,用来创建xmlhttprequest对象
function creatReq() // 创建xmlhttprequest,ajax开始
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
var url="ajaxServer.aspx"; //要请求的服务端地址
if(window.XMLHttpRequest) //非IE浏览器及IE7(7.0及以上版本),用xmlhttprequest对象创建
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
req=new XMLHttpRequest();
}
else if(window.ActiveXObject) //IE(6.0及以下版本)浏览器用activexobject对象创建,如果用户浏览器禁用了ActiveX,可能会失败.
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
req=new ActiveXObject("Microsoft.XMLHttp");
}
if(req) //成功创建xmlhttprequest
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
req.open("GET",url,true); //与服务端建立连接(请求方式post或get,地址,true表示异步)
req.onreadystatechange = callback; //指定回调函数
req.send(null); //发送请求
}
}
function callback() //回调函数,对服务端的响应处理,监视response状态
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if(req.readystate==4) //请求状态为4表示成功
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if(req.status==200) //http状态200表示OK
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Dispaly(); //所有状态成功,执行此函数,显示数据
}
else //http返回状态失败
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
alert("服务端返回状态" + req.statusText);
}
}
else //请求状态还没有成功,页面等待![](https://www.cnblogs.com/Images/dot.gif)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
document .getElementById ("myTime").innerHTML ="数据加载中
";
}
}
function Dispaly() //接受服务端返回的数据,对其进行显示
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
document .getElementById ("myTime").innerHTML =req.responseText;
}
</
script
>
</
head
>
<
body
>
<
div
id
="myTime"
></
div
>
<
input
id
="Button1"
type
="button"
value
="Get Time"
onclick
="creatReq();"
/>
</
body
>
</
html
>
服务端ajaxServer.aspx代码 (不需要HTML代码,只需服务端处理返回数据,也可用ashx文件来处理)
public
partial
class
ajaxServer : System.Web.UI.Page
![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
protected void Page_Load(object sender, EventArgs e)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
System.Threading.Thread.Sleep(1000); //为了看到ajax效果,将当前线程延时1000毫秒
Response.Write(DateTime .Now .ToString ()); //输出当前时间
}
}
运行效果:
![](https://i-blog.csdnimg.cn/blog_migrate/794aaf7dca0dcbeb8e361b9bffcad8d9.jpeg)
![](https://i-blog.csdnimg.cn/blog_migrate/041a6023a3d2a8547afe30221a97164d.jpeg)
转载于:https://www.cnblogs.com/ericgisser/archive/2010/06/25/ajax_xmlhttprequest.html