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()?
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()?
Share Improve this question asked Jun 24, 2016 at 0:46 Sarkis ArutiunianSarkis Arutiunian 1,2913 gold badges17 silver badges36 bronze badges 3- 1 The documentation for using fetch might be helpful. – showdev Commented Jun 24, 2016 at 0:48
- no, I still can't get body, maybe to tired) but docs doesn't help in this case – Sarkis Arutiunian Commented Jun 24, 2016 at 0:49
- You have backticks instead of quotes in your jQuery code, maybe you made the same mistake with fetch. – Barmar Commented Jun 24, 2016 at 0:51
1 Answer
Reset to default 12This 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