如何解析 javascript 中的数据
how to parse data in javascript
使用免费的 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>
使用免费的 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>