Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I stuck with one small issue. Rewriting js file from jQuery to native JS, and in jQuery we use:

$.get(`/page`, function (data) {
        elem.html(data);
}

basically we fetching body from '/page' and pushing it to elem.innerHTML.

But how I can get html body using fetch() instead of .get()?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
131 views
Welcome To Ask or Share your Answers For Others

1 Answer

This looks like the equivalent:

fetch('/page').then(function(response) {
    return response.text();
}).then(function(string) {
    elem.innerHTML = string;
});

fetch() returns a promise that resolves to a Response object. The text() method of the Response returns a promise that resolves to the body of the response as a string. You then put that string into the HTML.

DEMO


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...