1

XMLを返すAPIを呼び出すJSを書いています。私は 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'"コードが実行され、呼び出しで終了すると、JS エラーが発生しgetElementます。Mootools は、私が理解していることから、g​​etElement メソッドをすべての Document オブジェクトに提供します。これは response_xml です。ただし、これを行うと:

Object.getOwnPropertyNames(response_xml);

getElement返されるプロパティのリストのどこにも見つかりません。それがなぜなのかについてのアイデアはありますか?

4

1 に答える 1

3

良い。これは、メモリ内に存在するが Element プロトタイプから継承しない 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 / non-strict doctype で動作するはずです。Request.HTML を使用していることに注意してください

于 2013-06-13T08:50:25.820 に答える