как разобрать данные в javascript - PullRequest
0 голосов
/ 29 апреля 2020

Используйте бесплатный REST API: https://jsonplaceholder.typicode.com/, чтобы получить 100 альбомов. И отобразить все альбомы на странице html следующим образом:

 UserId: value of userId from the object that came to you,
 Id: Id value from the object that came to you,
 Title: title value from the object that came to you
 As a result, 100 different albums should be parsed on your page.

Я попытался проанализировать данные, и данные стали объектом JavaScript с использованием JSON .parse (), но данные отображаются в json формат

<h2>The XMLHttpRequest Object</h2>
<button type="button" onclick="loadDoc()">Change Content</button>
</div>

<script>
function loadDoc() {
 const xhttp = new XMLHttpRequest();
 const url = "https://jsonplaceholder.typicode.com/albums";
 xhttp.onreadystatechange = function() {
   if (this.readyState == 4 && this.status == 200) {
     document.getElementById("demo").innerHTML =
     this.responseText;
   }
 };

 xhttp.open("GET", url, true);
 xhttp.send();
}
</script>

1 Ответ

1 голос
/ 29 апреля 2020

Работает нормально, если добавить <div id="demo"></div>:

function loadDoc() {
 const xhttp = new XMLHttpRequest();
 const url = "https://jsonplaceholder.typicode.com/albums";
 xhttp.onreadystatechange = function() {
   if (this.readyState == 4 && this.status == 200) {
     document.getElementById("demo").innerHTML =
     this.responseText;
   }
 };

 xhttp.open("GET", url, true);
 xhttp.send();
}
<h2>The XMLHttpRequest Object</h2>
<button type="button" onclick="loadDoc()">Change Content</button>

<div id="demo"></div>
...