2017-08-04 1 views
4

XHRのresponseType='document'は素晴らしいです、などに:fetch()はresponseType = documentを実行できますか?それが戻ってあなたあなたがquerySelectorを使用することができますDOMドキュメント手渡すため

var xhr = new XMLHttpRequest(); 
xhr.open('GET', '/', true); 
xhr.responseType = 'document'; 
xhr.onload = function(e) { 
    var document = e.target.response; 
    var h2headings = document.querySelectorAll('h2'); 
    // ... 
}; 

が、これはfetch方法で可能ですか?

答えて

4

APIは、Webブラウザ(see discussion)であることに依存関係のない純粋なネットワーク層のAPIがあるとして、それはネイティブfetchではサポートされていないが、やってのけるには余りにも難しいことではありません。

fetch('/').then(res => res.text()) 
    .then(text => new DOMParser().parseFromString(text, 'text/html')) 
    .then(document => { 
    const h2headings = document.querySelectorAll('h2'); 
    // ... 
    }); 
関連する問題