欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

javascript对XMLHttpRequest异步请求的面向对象封装

程序员文章站 2022-03-04 16:56:21
复制代码 代码如下: function callbackobject() { this.xmlhttp = this.gethttpobject(); } callback...
复制代码 代码如下:

function callbackobject()
{
this.xmlhttp = this.gethttpobject();
}
callbackobject.prototype.gethttpobject = function() //动态为callbackobject的原型添加了gethttpobject共有方法
{
//第一步:创建xmlhttprequest对象
//进行兼容性判断
var xmlhttp;
/*@cc_on
@if (@_jscript_version >= 5)
try {
xmlhttp = new activexobject("msxml2.xmlhttp");
} catch (e) {
try {
xmlhttp = new activexobject("microsoft.xmlhttp");
} catch (e) {
xmlhttp = false;
}
}
@else
xmlhttp = false;
@end @*/
if (!xmlhttp && typeof xmlhttprequest != 'undefined') {
try {
xmlhttp = new xmlhttprequest();
} catch (e) {
xmlhttp = false;
}
}
return xmlhttp;
}
callbackobject.prototype.docallback = function(url)
{
if( this.xmlhttp )
{
if( this.xmlhttp.readystate == 4 || this.xmlhttp.readystate == 0 )
{
var othis = this;
//第二步:注册回调方法,当服务器处理结束返回数据以后利用回调方法实现局部的页面刷新数据
//这个回调方法实际上在每次xmlhttprequest对象的readystate属性的值发生变化的时候都会被调用
this.xmlhttp.onreadystatechange = function() {
//根据xmlhttp.readystate返回值不同调用不同的方法。
othis.readystatechange();
};
//第三步:设置和服务器交互的相应参数
this.xmlhttp.open('post', url);
this.xmlhttp.setrequestheader('content-type', 'application/x-www-form-urlencoded');
//第四步:设置向服务器发送的数据,启动和服务器端交互
this.xmlhttp.send(null);
}
}
}
callbackobject.prototype.abortcallback = function()
{
if( this.xmlhttp )
this.xmlhttp.abort();
}
callbackobject.prototype.readystatechange = function() {
//第五步:判断和服务器交互是否完成,还要判断服务器端是否正确返回数据
//this.xmlhttp.readystate == 0初始化状态。xmlhttprequest 对象已创建或已被 abort() 方法重置。
if (this.xmlhttp.readystate == 1) {
//open() 方法已调用,但是 send() 方法未调用。请求还没有被发送。
this.onloading();
}
else if (this.xmlhttp.readystate == 2) {
//send() 方法已调用,http 请求已发送到 web 服务器。未接收到响应。
this.onloaded();
}
else if (this.xmlhttp.readystate == 3) {
//receiving 所有响应头部都已经接收到。响应体开始接收但未完成。
this.oninteractive();
}
else if (this.xmlhttp.readystate == 4) {
//loaded http 响应已经完全接收。
if (this.xmlhttp.status == 0)
this.onabort();
else if (this.xmlhttp.status == 200 && this.xmlhttp.statustext == "ok")
this.oncomplete(this.xmlhttp.responsetext, this.xmlhttp.responsexml);
else
this.onerror(this.xmlhttp.status, this.xmlhttp.statustext, this.xmlhttp.responsetext);
}
}
callbackobject.prototype.onloading = function()
{
// loading
}
callbackobject.prototype.onloaded = function()
{
// loaded
}
callbackobject.prototype.oninteractive = function()
{
// interactive
}
callbackobject.prototype.oncomplete = function(responsetext, responsexml)
{
// complete
}
callbackobject.prototype.onabort = function()
{
// abort
}
callbackobject.prototype.onerror = function(status, statustext)
{
// error
}


调用方法如下:
复制代码 代码如下:

<script type="text/javascript">
function createrequest()
{
var name = escape(document.getelementbyid("name").value);
var cbo = new callbackobject();
cbo.oncomplete = cbo_complete;
cbo.onerror = cbo_error;
cbo.onloaded = onloading;
cbo.docallback("ajaxtest.aspx?name=" + name);
}

function onloading() {
alert("onloading " );
}

function cbo_complete(responsetext, responsexml)
{
alert("成功 "+responsetext);
}

function cbo_error(status, statustext, responsetext)
{
alert(responsetext);
}
</script>

onreadystatechange事件
  无论readystate值何时发生改变,xmlhttprequest对象都会激发一个readystatechange事件。其中,onreadystatechange属性接收一个eventlistener值-向该方法指示无论readystate值何时发生改变,该对象都将激活。
  responsetext属性
  这个responsetext属性包含客户端接收到的http响应的文本内容。当readystate值为0、1或2时,responsetext包含一个空字符串。当readystate值为3(正在接收)时,响应中包含客户端还未完成的响应信息。当readystate为4(已加载)时,该responsetext包含完整的响应信息。
  responsexml属性
  此responsexml属性用于当接收到完整的http响应时(readystate为4)描述xml响应;此时,content-type头部指定mime(媒体)类型为text/xml,application/xml或以+xml结尾。如果content-type头部并不包含这些媒体类型之一,那么responsexml的值为null。无论何时,只要readystate值不为4,那么该responsexml的值也为null。
  其实,这个responsexml属性值是一个文档接口类型的对象,用来描述被分析的文档。如果文档不能被分析(例如,如果文档不是良构的或不支持文档相应的字符编码),那么responsexml的值将为null。
  status属性
  这个status属性描述了http状态代码,而且其类型为short。而且,仅当readystate值为3(正在接收中)或4(已加载)时,这个status属性才可用。当readystate的值小于3时试图存取status的值将引发一个异常。
  statustext属性
  这个statustext属性描述了http状态代码文本;并且仅当readystate值为3或4才可用。当readystate为其它值时试图存取statustext属性将引发一个异常。