V
V
Victor L2017-02-13 17:06:44
JavaScript
Victor L, 2017-02-13 17:06:44

How to correctly display data on the page?

"Asynchronous" :)
Tell me how it should work in general to get the data and display it on the page

(async() => {
  try {
    let response = await fetch("https://jsonplaceholder.typicode.com/posts/1", {  
        method: 'GET',
        headers: {
          'Accept': 'application/json',
          'Content-Type': 'application/json'
        }
  });
  	let data = await response.json();
        console.log(data);
        const tmpl = items => `
        <table>
        ${items.map(item => `
            <tr><td>${item.userId}</td></tr>
            <tr><td>${item.id}</td></tr>
            <tr><td>${item.title}</td></tr>
            <tr><td>${item.body}</td></tr>
        `).join('')}
        </table>
    `;
    let body = document.getElementsByTagName('body');
    document.body.innerHTML += tmpl(data);
    
  } catch (e) {
    console.log("  ¯\_(ツ)_/¯  Что то не так!");
  }
})();

Sandbox

Answer the question

In order to leave comments, you need to log in

2 answer(s)
X
xmoonlight, 2017-02-13
@xmoonlight

https://sitecoder.blogspot.ru/2017/01/rex-tiny-tem...

R
Rustamka Vorontsov, 2017-02-13
@rmfordev

https://jsfiddle.net/fbxh8gb6/3/

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question