Can fetch () do responseType = document?

XHR responseType='document'is awesome because it returns you a DOM document that you can use querySelector, etc.:

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');
  // ...
};

Is this possible with a method fetch?

+4
source share
1 answer

It is not supported internally fetch, since the API is just a network API, independent of being in a web browser ( see discussion ), but it is not too difficult to do:

fetch('/').then(res => res.text())
  .then(text => new DOMParser().parseFromString(text, 'text/html'))
  .then(document => {
    const h2headings = document.querySelectorAll('h2');
    // ...
  });
+4
source

Source: https://habr.com/ru/post/1683033/


All Articles