fetch API returning an empty string

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

Leave a Comment