gpt4 book ai didi

javascript - Mootools:getElement 方法在 XML AJAX 响应中不可用

转载 作者:行者123 更新时间:2023-11-30 13:02:00 26 4
gpt4 key购买 nike

我正在编写一些 JS,它调用一个返回一些 XML 的 API。我在使用 Mootools 1.3(具有兼容性)和 Mootools-More 1.4。 没有使用其他 JS 框架。我的代码如下所示:

var req = new Request({url:'whatevs.com/api', method:'get'}).addEvent('success',
function(response_text, response_xml) {
alert(response_xml.getElement('response').getProperty('status'));
}).send();

实际的 API 调用成功,response_text 如下所示:

<?xml version="1.0" ?>
<response status="success" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://whatevs.com/api/api.xsd">
<resultset total="0" start_index="0" results_limit="20" query="keyword=cats">
<items/>
</resultset>
</response>

但是,我得到一个 "Uncaught TypeError: Object #<Document> has no method 'getElement'"代码运行并死于 getElement 时出现 JS 错误称呼。据我了解,Mootools 为所有 Document 对象提供了一个 getElement 方法,即 response_xml。但是,当我这样做时:

Object.getOwnPropertyNames(response_xml);

getElement在返回的属性列表中无处可寻。关于为什么会这样有什么想法吗?

最佳答案

嗯。这将返回一个存在于内存中但不继承自 Element 原型(prototype)的 nodeList。您可以通过从 Array.prototype 调用来遍历节点列表并使用 .filter 等,但 DOM 更擅长查找元素,因此您最好使用代理元素。见http://jsfiddle.net/cjXMN/

new Request.HTML({
url: '/echo/html/',
data: {
html: document.getElement('textarea').get('value')
},
onComplete: function(){
console.log(this.response.elements);
// or set the html to this.response.text etc.
var proxy = new Element('div').adopt(this.response.elements);
console.log(proxy.getElement('response').get('status'));
}
}).send();

它很可能适用于 html5/非严格的文档类型。注意我正在使用 Request.HTML

关于javascript - Mootools:getElement 方法在 XML AJAX 响应中不可用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17076109/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com