在下面的代码示例中,我想遍历responseText对象,该对象包含来自request_page.php文件的html代码。在onSuccess事件中,我想检查是否< Div> id为“ersDiv”时发布了任何错误。
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文件是这样的:
<div align='center'><div id='ersDiv'>Page loaded with insufficient data</div></div>
答案 0 :(得分:1)
function(responseText, responseXML) {
if (responseText.getElement('#ersDiv').get('text') === 'Page loaded with insufficient data'){
console.log('There was an error');
}
}
顺便说一下。 1990年,他们希望他们的align='center'
回来: - )
答案 1 :(得分:1)
尝试1.2.x(为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();
工作示例:
http://www.jsfiddle.net/dimitar/vLgqB/
1.3中的可以像oskar建议的那样工作:
console.log($$(this.response.tree).getElement("#ersDiv")[0].get("text"));
http://www.jsfiddle.net/dimitar/vLgqB/2/
玩得开心。