随便写了一个ajax对象类,可以如果一个页面多个请求的时候,前面的请求会被后面的覆盖。
后来换了一种写法,在getRequest()中单独声明一个ajax对象可以正常。
但是,很奇怪,不知道为什么用了对象类还会出这种问题。请达人帮我看一下谢谢。想知道其中的缘由,我印象中对象本身的所有属性不就都是单独的吗? 怎么会被覆盖呢?var ZHCAjax = {};//声明对象
ZHCAjax.AJAX = false;//ajax实例对象
ZHCAjax.URL;
ZHCAjax.input;//ajax返回值插入的域
ZHCAjax.CreateAJAX = function(){//注意方法和变量的区分
try

 ZHCAjax.AJAX = new ActiveXObject("Msxml2.XMLHTTP");
}
catch(e)
{
 try
 {
   ZHCAjax.AJAX = new ActiveXObject("Microsoft.XMLHTTP");
 }
 catch(e2)
 {
   ZHCAjax.AJAX = false;
 }
}
};
ZHCAjax.getRequest = function(callfunction){//callfunction为接受response的程序
   ZHCAjax.CreateAJAX();//实例化一个ajax对象
   ZHCAjax.AJAX.open("GET",ZHCAjax.URL,true);
   ZHCAjax.AJAX.setRequestHeader('Content-Type','utf-8');   
   ZHCAjax.AJAX.onreadystatechange = function(){
  if(ZHCAjax.AJAX.readyState==2)
  {
document.getElementById("你好").innerHTML =  "数据正在处理中..."; 
  }
  else if(ZHCAjax.AJAX.readyState==4) 
  {
var response = ZHCAjax.AJAX.responseText;
document.getElementById(ZHCAjax.input).innerHTML =  unescape(response);
  }
};
   ZHCAjax.AJAX.send(null); 
};

解决方案 »

  1.   

    哦 对象直接量的方法都是静态的? 我还以为是默认instance一个对象然后调用方法和属性呢..
    迷糊了。谢谢~
      

  2.   

    LS说的对var ZHCAjax = function(){
    this.AJAX = false;//ajax实例对象 
    this.URL;
    this.input; //ajax返回值插入的域 
    }
    ZHCAjax.prototype = {
    CreateAJAX: function(){//注意方法和变量的区分 
    try {
    this.AJAX = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e) {
    try {
    this.AJAX = new ActiveXObject("Microsoft.XMLHTTP");
    } catch (e2) {
    this.AJAX = false;
    }
    }
    },
    getRequest: function(callfunction){//callfunction为接受response的程序 
    this.CreateAJAX();//实例化一个ajax对象 
    this.AJAX.open("GET", ZHCAjax.URL, true);
    this.AJAX.setRequestHeader('Content-Type', 'utf-8');
    this.AJAX.onreadystatechange = function(){
    if (this.AJAX.readyState == 2) {
    document.getElementById("你好").innerHTML = "数据正在处理中...";
    } else if (this.AJAX.readyState == 4) {
    var response = this.AJAX.responseText;
    document.getElementById(ZHCAjax.input).innerHTML = unescape(response);
    }
    };
    this.AJAX.send(null);
    }
    };
      

  3.   


    谢谢,我还以为对象直接量是默认instance一个隐藏对象呢 
      

  4.   

    <html>
    <body>
    <span id="你好"></span>
    <div id="b" style="width:800px; height:200px; overflow:scroll"></div>
    <div id="g" style="width:800px; height:200px; overflow:scroll"></div>
    <script type="text/javascript">
    function ZHCAjax(url, input){
    var ZHCAjax = {};//声明对象 
    this.AJAX = false;//ajax实例对象 
    this.URL = url; 
    this.input = input;//ajax返回值插入的域 
    this.CreateAJAX = function(){//注意方法和变量的区分 
    try{ 
    this.AJAX = new ActiveXObject("Msxml2.XMLHTTP"); 
    }catch(e){ 
    try{ 
    this.AJAX = new ActiveXObject("Microsoft.XMLHTTP"); 
    }catch(e2){ 
    this.AJAX = false; 


    }; 
    }
    ZHCAjax.prototype = {
    getRequest : function(callfunction){//callfunction为接受response的程序 
    this.CreateAJAX();//实例化一个ajax对象 
    this.AJAX.open("GET",this.URL,true); 
    this.AJAX.setRequestHeader('Content-Type','utf-8'); 
    var self = this; 
    this.AJAX.onreadystatechange = function(){ 
      if(self.AJAX.readyState==2) 
      { 
    document.getElementById("你好").innerHTML =  "数据正在处理中..."; 
      } 
      else if(self.AJAX.readyState==4) 
      { 
    var response = self.AJAX.responseText; 
    document.getElementById(self.input).innerHTML = unescape(response); 
      } 
    }; 
      this.AJAX.send(null); 
    }
    }
    var ajax = new ZHCAjax("http://www.baidu.com", "b");
    var ajax2 = new ZHCAjax("http://www.google.cn", "g");
    ajax.getRequest();
    ajax2.getRequest();
    </script>
    </body>
    </html>
      

  5.   

    楼上两为达人的方法,在验证状态的时候找不到 this.AJAX.readyState 
      

  6.   

    看来getRequest中的ajax对象要单独声明类中的.AJAX这个属性貌似是不正确的使用
      

  7.   


    哦  谢谢 看到了 你用了个var self = this;   这个是相当于指针的作用吧?
      

  8.   

    不是指针,js里没有指针的概念
    self是对当前对象的引用,还有就是变量作用域的问题
      

  9.   

    参见easyui.ajax部分:
    ajax:{//xmlhttp request
    tryList:function(){
    var xhr = null;
    for(var i=0;i<arguments.length;i++){
    var lambda = arguments[i];
    try{xhr = lambda();}catch(e){}
    if(xhr){break;}
    }
    return xhr;
    },
    init:function(){
    return this.tryList(
    function(){try{return new ActiveXObject('MSXML2.XMLHttp.6.0');}catch(e){}},
    function(){try{return new ActiveXObject('MSXML2.XMLHttp.3.0');}catch(e){}},
    function(){try{return new XMLHttpRequest();}catch(e){}},
    function(){try{return new ActiveXObject('MSXML2.XMLHttp.5.0');}catch(e){}},
    function(){try{return new ActiveXObject('MSXML2.XMLHttp.4.0');}catch(e){}},
    function(){try{return new ActiveXObject('Msxml2.XMLHTTP');}catch(e){}},
    function(){try{return new ActiveXObject('MSXML.XMLHttp');}catch(e){}},
    function(){try{return new ActiveXObject('Microsoft.XMLHTTP');}catch(e){}}
    ) || null;
    },
    post:function(sUrl,sArgs,bAsync,fCallBack,fFailure){
    var xhr = this.init();
    if(!xhr){alert('XmlHttp对象未就绪!');return;}
    xhr.onreadystatechange = function(){
    if(xhr.readyState == 4){
    if(xhr.status == 200){
    if(fCallBack&&fCallBack.constructor==Function){fCallBack(xhr);}
    }else{
    if(fFailure&&fFailure.constructor==Function){
    fFailure(xhr);
    }else{
    alert('服务器错误:'+xhr.status);
    }
    xhr = null;
    }
    }
    };
    xhr.open('POST',encodeURI(sUrl),bAsync);
    xhr.setRequestHeader('Content-Length',sArgs.length);
    xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
    xhr.send(sArgs);
    },
    get:function(sUrl,bAsync,fCallBack,fFailure){
    var xhr = this.init();
    if(!xhr){alert('XmlHttp对象未就绪!');return;}
    xhr.onreadystatechange = function(){
    if(xhr.readyState == 4){
    if(xhr.status == 200){
    if(fCallBack&&fCallBack.constructor==Function){fCallBack(xhr);}
    }else{
    if(fFailure&&fFailure.constructor==Function){
    fFailure(xhr);
    }else{
    alert('服务器错误:'+xhr.status);
    }
    xhr = null;
    }
    }
    };
    xhr.open('GET',encodeURI(sUrl),bAsync);
    xhr.send('Null');
    },
    xRequest:function(id,url){//取跨域数据,script标签id,目标url
    oScript = document.getElementById(id);
    var head = document.getElementsByTagName("head").item(0);
    if (oScript){head.removeChild(oScript);}
    oScript = document.createElement("script");
    oScript.setAttribute("src", url);
    oScript.setAttribute("id",id);
    oScript.setAttribute("type","text/javascript");
    oScript.setAttribute("language","javascript");
    head.appendChild(oScript);
    return oScript;
    }
    }