gpt4 book ai didi

javascript - 如何在javascript中解析数据

转载 作者:行者123 更新时间:2023-12-02 20:54:55 25 4
gpt4 key购买 nike

使用免费的 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.

我尝试解析数据,数据使用 JSON.parse() 变成 JavaScript 对象,但数据以 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>

最佳答案

如果添加<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>

关于javascript - 如何在javascript中解析数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61510443/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com