D
D
Derbakov2020-04-27 16:52:55
JavaScript
Derbakov, 2020-04-27 16:52:55

How to display the received data on the page?

You need to use the free REST API: https://jsonplaceholder.typicode.com/ to get 100 albums, and display them on the page in the form:

UserId : userId value from the object that came to you,
Id : Id value from the object that came to you,
Title : value title from the object that came to you

I understand that I can display it in the console, but not on the screen:

const xhr = new XMLHttpRequest();
const url = ' https://jsonplaceholder.typicode.com/albums ';

xhr.open('GET', url);

xhr.addEventListener('load', () => {
console.log(JSON.parse(xhr.response));
});

xhr.send();

Answer the question

In order to leave comments, you need to log in

1 answer(s)
0
0xD34F, 2020-04-27
@Derbakov

fetch('https://jsonplaceholder.typicode.com/albums')
  .then(r => r.json())
  .then(r => {
    document.body.insertAdjacentHTML('beforeend', `
      <div class="wrapper">${r.map(n => `
        <div class="item">
          <div>UserId: ${n.userId}</div>
          <div>Id: ${n.id}</div>
          <div>Title: ${n.title}</div>
        </div>`).join('')}
      </div>
    `);
  });

.item {
  border: 1px solid silver;
  padding: 10px;
  margin: 10px;
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question