fetch API to get HTML response

后端 未结 2 1830
面向向阳花
面向向阳花 2020-11-30 03:33

I am trying to get a page\'s HTML using fetch API. Here is my code.

var quizUrl = \'http://www.lipsum.com/\';
var myHeaders = new Headers();
myHeaders.append         


        
2条回答
  •  盖世英雄少女心
    2020-11-30 04:22

    I guess this might help, use as below:

    fetch('/url/to/server')
    .then(res => {
        return res.text();
    })
    .then(data => {
        $('#container').html(data);
    });
    

    And in server side, return content as plain text without setting header content-type.

    I used $('#container') to represent the container that you want the html data to go after retrieving it.

    The difference with fetching json data is using res.json() in place of res.text() And also, don't append any headers

提交回复
热议问题