日期:2014-05-16  浏览次数:20365 次

怎样建立XMLHTTP对象
function   Ajax(){
this.getXmlhttp   =   new   ActiveXObject( "Msxml2.XMLHTTP ");
}

var   xmlGet   =   Ajax();

我想用oo方式编写
但这里总是说xmlGet.getXmlhttp为空或不是对象

第一次用这种方式写
请教各位了


------解决方案--------------------
没看出来你的CODE的的用途..
那个错误是因为this对象的引用的问题..

<script type= "text/javascript ">
function createXMLHTTP() {
//返回XMLHTTP对象
if (window.ActiveXObject)
return new ActiveXObject( "Microsoft.XMLHTTP ");
else if (window.XMLHttpRequest)
return new XMLHttpRequest();
}
function Ajax(){
var o = this;
o.getXmlhttp = createXMLHTTP();
o.onComplete = function () {};
o.onError = function () {};
o.getXmlhttp.onreadystatechange = function () {
Ajax.prototype.statechange.call(o);
}
}

Ajax.prototype.statechange = function () {
if(this.getXmlhttp.readyState == 4)
this.onComplete();
}

Ajax.prototype.readyState = function() {
return this.getXmlhttp.readyState;
}

Ajax.prototype.onSend = function(url) {
this.getXmlhttp.open( "GET ", url, true);
this.getXmlhttp.send(null);
}

Ajax.prototype.Text = function() {
return this.getXmlhttp.responseText;
}

Ajax.prototype.Abort = function() {
this.getXmlhttp.abort();
}

var boxs = new Ajax();
boxs.onComplete = function (){
alert(boxs.Text());
};
boxs.onSend( '1.asp ');
</script>