Skip to content Skip to sidebar Skip to footer

$.get() And Fetch() Getting Html Body

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 b

Solution 1:

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

Post a Comment for "$.get() And Fetch() Getting Html Body"