Search code examples
javascriptmootoolsmootools1.2

MooTools: How to use responseText directly


In following example of code, I want to traverse the responseText object which consist the html code came from request_page.php file. In onSuccess event, i want to check whether < Div > with id 'ersDiv' has any errors posted in it.

new Request.HTML({
    url: 'request_page.php',
    onSuccess: function(responseText, responseXML) {
    // My expected code to handle responseText object
    alert(errorMessage);
    },
    onFailure: function() {  }
});

request_page.php file is like this :

<div align='center'><div id='ersDiv'>Page loaded with insufficient data</div></div>

Solution

  • try this for 1.2.x (example tweaked for the jsfiddle api):

    new Request.HTML({
        url: '/echo/html/',
        method: "post",
        data: {
            html: "<div align='center'><div id='ersDiv'>Page loaded with insufficient data</div></div>",
            delay: 1
        },
        onComplete: function(responseText, responseXML) {
            var error, errors = responseXML.getElements("div").filter(function(el) {
                return el.get("id") == "ersDiv";
            });
    
            if (errors.length) {
                error = errors[0].get("text");
                alert(error);
            }
        }
    }).send();
    

    working example:

    http://www.jsfiddle.net/dimitar/vLgqB/

    in 1.3 this can work as oskar suggested:

    console.log($$(this.response.tree).getElement("#ersDiv")[0].get("text"));
    

    http://www.jsfiddle.net/dimitar/vLgqB/2/

    have fun.