Heim  >  Artikel  >  Web-Frontend  >  Objektorientierte JavaScript-Kapselung asynchroner XMLHttpRequest-Anfragen

Objektorientierte JavaScript-Kapselung asynchroner XMLHttpRequest-Anfragen

高洛峰
高洛峰Original
2017-01-04 09:28:561124Durchsuche

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 
}

Die aufrufende Methode lautet wie folgt:

<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-Ereignis
Immer wenn sich der readyState-Wert ändert, löst das XMLHttpRequest-Objekt ein readystatechange-Ereignis aus. Unter anderem empfängt das Attribut onreadystatechange einen EventListener-Wert, der der Methode anzeigt, dass das Objekt immer dann aktiviert wird, wenn sich der readyState-Wert ändert.
ResponseText-Attribut
Dieses ResponseText-Attribut enthält den Textinhalt der vom Client empfangenen HTTP-Antwort. Wenn der readyState-Wert 0, 1 oder 2 ist, enthält ResponseText eine leere Zeichenfolge. Wenn der readyState-Wert 3 (Empfangen) ist, enthält die Antwort die unvollständigen Antwortinformationen des Clients. Wenn readyState 4 (geladen) ist, enthält der Antworttext vollständige Antwortinformationen.
ResponseXML-Attribut
Dieses ResponseXML-Attribut wird verwendet, um die XML-Antwort zu beschreiben, wenn eine vollständige HTTP-Antwort empfangen wird (readyState ist 4). Zu diesem Zeitpunkt gibt der Content-Type-Header den MIME-Typ (Medien) als Text/ an. xml, application /xml oder endend mit +xml. Wenn der Content-Type-Header keinen dieser Medientypen enthält, ist der Wert von „responseXML“ null. Immer wenn der readyState-Wert nicht 4 ist, ist der Wert von ResponseXML ebenfalls null.
Tatsächlich handelt es sich bei diesem ResponseXML-Attributwert um ein Objekt vom Typ Dokumentschnittstelle, das zur Beschreibung des analysierten Dokuments verwendet wird. Wenn das Dokument nicht analysiert werden kann (z. B. wenn das Dokument nicht wohlgeformt ist oder die entsprechende Zeichenkodierung des Dokuments nicht unterstützt), ist der Wert von „responseXML“ null.
Statusattribut
Dieses Statusattribut beschreibt den HTTP-Statuscode und hat einen kurzen Typ. Darüber hinaus ist dieses Statusattribut nur verfügbar, wenn der readyState-Wert 3 (Empfangen) oder 4 (Geladen) ist. Der Versuch, auf den Statuswert zuzugreifen, wenn readyState kleiner als 3 ist, löst eine Ausnahme aus.
StatusText-Attribut
Dieses statusText-Attribut beschreibt den HTTP-Statuscodetext und ist nur verfügbar, wenn der readyState-Wert 3 oder 4 ist. Der Versuch, auf die Eigenschaft „statusText“ zuzugreifen, wenn „readyState“ einen anderen Wert hat, löst eine Ausnahme aus.


Weitere Artikel zur objektorientierten Kapselung asynchroner XMLHttpRequest-Anfragen durch JavaScript finden Sie auf der chinesischen PHP-Website!


Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn