最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - $.get() and fetch() getting HTML body - Stack Overflow

programmeradmin3浏览0评论

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
Add a ment  | 

1 Answer 1

Reset to default 12

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

发布评论

评论列表(0)

  1. 暂无评论