Returning HTML With fetch()

后端 未结 5 1871
暗喜
暗喜 2020-11-28 03:45

I\'m trying to fetch a file and return it\'s HTML. However it\'s not as simple as I\'d have imagined.

    fetch(\'/path/to/file\')
    .then(function (respon         


        
5条回答
  •  猫巷女王i
    2020-11-28 04:12

    you can return the response with .text(), and then render the page in the doc as you want.

    function fetchHtml() {
      fetch('./file.html')
      .then((response) => {
        return response.text();
      })
      .then((html) => {
        document.body.innerHTML = html     
      });
    }
    

提交回复
热议问题