Domanda

Nel seguente esempio di codice, voglio attraversare l'oggetto responseText che consistono il codice html è venuto da file request_page.php. Nel caso in onSuccess, voglio verificare se

con id 'ersDiv' ha postato eventuali errori in esso.

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

file request_page.php è come questo:

<div align='center'><div id='ersDiv'>Page loaded with insufficient data</div></div>
È stato utile?

Soluzione

Prova questo per 1.2.x (ad esempio ottimizzato per l'API jsfiddle):

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

esempio di lavoro:

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

in 1.3 Questo può funzionare come Oskar suggerito:

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

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

divertirsi.

Altri suggerimenti

function(responseText, responseXML) {
  if (responseText.getElement('#ersDiv').get('text') === 'Page loaded with insufficient data'){
    console.log('There was an error');
  }
}

Btw. 1990 ha chiesto, vogliono indietro il loro align='center': -)

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top