StiGMaT
StiGMaT

Reputation: 760

Mootool ajax readystate response is always 1?

I've been trying to make this mootools (ver: 1.2.4) Class to process my ajax requests. But my scripts only returns readystate 1. never gets to 2,3 ,or 4, the method handleHttpResponse only seems to run once. I'ves put alerts to see and I only get 1. any ideas?

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;
    }
});

This is how i launch it for now. my content is null but my url is a working XML file. so it shouldnt be blank... ?

<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>

Tested on Google chrome, Firefox 4, Internet explorer 7 & 8, all same result. Here is an example of XML the script is suppose to output: http://jerenovici.net/gdwcommreply_genxml.php?getfield=idvideo&fieldid=64&parentid=59 so I know my php generating the xml is fine.

Thanks!!

Upvotes: 1

Views: 420

Answers (1)

stecb
stecb

Reputation: 14766

Why are you reinventing the wheel? If you're using mootools, making an ajax request is extremely easy (docs, referring to newest version, but in this case Request hasn't changed):

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();

Then, are you sure the url is correct?

Upvotes: 4

Related Questions