2011-05-13 67 views
1

我一直在试图让这个mootools(版本:1.2.4)类来处理我的ajax请求。但我的脚本只返回readystate 1.永远不会得到2,3或4,方法handleHttpResponse似乎只运行一次。我会发出警报,我只会得到1.任何想法?Mootool ajax readystate响应总是1?

var replyXML = new Class ({ 
    /* GDW AJAX REQUEST SCRIPT */ 
    /* By: Jonathan Robidas 2011-05-13 */ 
    initialize : function(url){ 
     this.http = this.getHTTPObject(); // We create the HTTP Object 
     this.url = url;          // Requested server-side script 
     this.response = '';        // String returned by AJAX, Set after positive response 
    }, 
    returnResponse : function() { 
     return this.response; 
    }, 
    handleHttpResponse : function() { 
     alert(this.http.readyState); 
     if (this.http.readyState == 4) { 
      if(this.http.status==200) { 
       alert("YA YA 2"); 
       this.response = this.http.responseText; 
       return true; 
      } 
     } 
    }, 
    requestXML : function() { 
     this.http.open("GET", this.url, true); 
     //this.http.onreadystateshange = this.handleHttpResponse(); 
     this.http.onload = this.handleHttpResponse(); 
    }, 
    getHTTPObject : function() { 
     var xmlhttp; 
     if(window.XMLHttpRequest){ 
      xmlhttp = new XMLHttpRequest(); 
     } 
     else if (window.ActiveXObject){ 
      xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); 
      if (!xmlhttp){ 
       xmlhttp=new ActiveXObject("Msxml2.XMLHTTP"); 
      } 
     } 
     return xmlhttp; 
    } 
}); 

这是我现在如何启动它。我的内容为空,但我的网址是一个可用的XML文件。所以它不应该是空白的...?

<script language="Javascript" type="text/Javascript"> 
    window.addEvent('domready', function() { 
     loadXML = new replyXML('gdwcommreply_genxml.php?getfield=idvideo&fieldid=64&parentid=59'); 
     loadXML.requestXML(); 
     content = loadXML.returnResponse(); 
     alert(content); 
     /* 
     x=content.documentElement.childNodes; 
     for (i=0;i<x.length;i++) { 
      document.write(x[i].nodeName); 
      document.write(": "); 
      document.write(x[i].childNodes[0].nodeValue); 
      document.write("<br />"); 
     } 
     */ 
    }); 
</script> 

上测试谷歌Chrome,火狐4,Internet Explorer 7的& 8,所有相同的结果。 下面是一个脚本应该输出的XML示例:http://jerenovici.net/gdwcommreply_genxml.php?getfield=idvideo&fieldid=64&parentid=59所以我知道我的php生成xml很好。

谢谢!

回答

4

你为什么重新发明轮子?如果您使用的mootools,使Ajax请求是非常容易的(docs,指的是最新版本,但在这种情况下,要求没有改变):

new Request({ 
    url : './gdwcommreply_genxml.php?getfield=idvideo&fieldid=64&parentid=59', 
    onSuccess : function(responseText, responseXML){ 

     /* here, do stuff with your response */ 

    }, 
    onFailure : function(xhr){ 

     /* the XMLHttpRequest instance. */ 

    } 
}).send(); 

然后,你确定的网址是否正确?

+0

我同意。 +1为务实。 – 2011-05-14 10:01:40

+0

好的,生病星期一试试。 – StiGMaT 2011-05-14 21:01:05

+0

感谢您使用这个班,首先尝试感谢您的帮助! – StiGMaT 2011-05-17 12:30:25