2011-05-13 63 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