AJAX 请求没有收到我想要的 属性
AJAX Request not receiving the property I want
我必须向以下 API 发送 AJAX 请求:https://jsonplaceholder.typicode.com/posts。然后按“标题”属性(升序)对结果进行排序并按 console.log
打印
这里的问题是我没有得到 title 属性,而是得到了很多这样的:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<div>
<h1>Getting Started With Fetch API</h1>
<button id="fetchJsonData">Fetch User Data</button>
</div>
<hr>
<div id="response"></div>
</body>
<script>
document.getElementById('fetchJsonData').addEventListener('click', fetchJson);
function fetchJson(){
fetch('https://jsonplaceholder.typicode.com/posts')
.then(response => response.json())
.then(json => console.log(json))
}
document.getElementById('fetchJsonData').addEventListener('click', fetchJson);
function fetchJson(){
fetch('https://jsonplaceholder.typicode.com/posts')
.then(response => response.json())
.then(titles => {
let output = '<h2>Lists of Titles</h2>';
// output += '<ul>';
titles.forEach(function(titles) {
output += `
<li>
${titles}
</li>
`;
output += '</ul>'
document.getElementById("response").innerHTML = output;
});
});
}
function sortList(ul) {
var ul = document.getElementById(ul);
Array.from(ul.getElementsByTagName("LI"))
.sort((a, b) => a.textContent.localeCompare(b.textContent))
.forEach(li => ul.appendChild(li));
}
sortList("response");
</script>
</html>
我访问数据的方式不对吗?请指教,提前致谢
在 forEach 循环中,${titles} 应该是 ${titles.title}
response是一个object数组,直接渲染需要获取title的字符串值
document.getElementById('fetchJsonData').addEventListener('click', fetchJson);
function fetchJson() {
fetch('https://jsonplaceholder.typicode.com/posts')
.then(response => response.json())
.then(json => console.log(json))
}
document.getElementById('fetchJsonData').addEventListener('click', fetchJson);
function fetchJson() {
fetch('https://jsonplaceholder.typicode.com/posts')
.then(response => response.json())
.then(titles => {
let output = '<h2>Lists of Titles</h2>';
output += '<ul>';
titles.sort((a, b) => a.title.localeCompare(b.title)).forEach((obj) => {
output += `<li>${obj.title}</li>`;
});
output += '</ul>';
document.getElementById("response").innerHTML = output;
});
}
<div>
<h1>Getting Started With Fetch API</h1>
<button id="fetchJsonData">Fetch User Data</button>
</div>
<hr>
<div id="response"></div>
这样操作。
阅读 docs 以获得更多见解 javascript。
document.getElementById('fetchJsonData')
.addEventListener('click', fetchJson);
function fetchJson(){
fetch('https://jsonplaceholder.typicode.com/posts')
.then(response => response.json())
.then(posts => {
let output = '<h2>Lists of Titles</h2>';
posts.sort((a, b) => a.title.localeCompare(b.title));
posts.forEach(function(post) {
output += `
<li>
${post.title}
</li>
`;
output += '</ul>'
document.getElementById("response").innerHTML = output;
});
});
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<div>
<h1>Getting Started With Fetch API</h1>
<button id="fetchJsonData">Fetch User Data</button>
</div>
<hr>
<div id="response"></div>
</body>
</html>
我必须向以下 API 发送 AJAX 请求:https://jsonplaceholder.typicode.com/posts。然后按“标题”属性(升序)对结果进行排序并按 console.log
打印这里的问题是我没有得到 title 属性,而是得到了很多这样的:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<div>
<h1>Getting Started With Fetch API</h1>
<button id="fetchJsonData">Fetch User Data</button>
</div>
<hr>
<div id="response"></div>
</body>
<script>
document.getElementById('fetchJsonData').addEventListener('click', fetchJson);
function fetchJson(){
fetch('https://jsonplaceholder.typicode.com/posts')
.then(response => response.json())
.then(json => console.log(json))
}
document.getElementById('fetchJsonData').addEventListener('click', fetchJson);
function fetchJson(){
fetch('https://jsonplaceholder.typicode.com/posts')
.then(response => response.json())
.then(titles => {
let output = '<h2>Lists of Titles</h2>';
// output += '<ul>';
titles.forEach(function(titles) {
output += `
<li>
${titles}
</li>
`;
output += '</ul>'
document.getElementById("response").innerHTML = output;
});
});
}
function sortList(ul) {
var ul = document.getElementById(ul);
Array.from(ul.getElementsByTagName("LI"))
.sort((a, b) => a.textContent.localeCompare(b.textContent))
.forEach(li => ul.appendChild(li));
}
sortList("response");
</script>
</html>
我访问数据的方式不对吗?请指教,提前致谢
在 forEach 循环中,${titles} 应该是 ${titles.title}
response是一个object数组,直接渲染需要获取title的字符串值
document.getElementById('fetchJsonData').addEventListener('click', fetchJson);
function fetchJson() {
fetch('https://jsonplaceholder.typicode.com/posts')
.then(response => response.json())
.then(json => console.log(json))
}
document.getElementById('fetchJsonData').addEventListener('click', fetchJson);
function fetchJson() {
fetch('https://jsonplaceholder.typicode.com/posts')
.then(response => response.json())
.then(titles => {
let output = '<h2>Lists of Titles</h2>';
output += '<ul>';
titles.sort((a, b) => a.title.localeCompare(b.title)).forEach((obj) => {
output += `<li>${obj.title}</li>`;
});
output += '</ul>';
document.getElementById("response").innerHTML = output;
});
}
<div>
<h1>Getting Started With Fetch API</h1>
<button id="fetchJsonData">Fetch User Data</button>
</div>
<hr>
<div id="response"></div>
这样操作。
阅读 docs 以获得更多见解 javascript。
document.getElementById('fetchJsonData')
.addEventListener('click', fetchJson);
function fetchJson(){
fetch('https://jsonplaceholder.typicode.com/posts')
.then(response => response.json())
.then(posts => {
let output = '<h2>Lists of Titles</h2>';
posts.sort((a, b) => a.title.localeCompare(b.title));
posts.forEach(function(post) {
output += `
<li>
${post.title}
</li>
`;
output += '</ul>'
document.getElementById("response").innerHTML = output;
});
});
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<div>
<h1>Getting Started With Fetch API</h1>
<button id="fetchJsonData">Fetch User Data</button>
</div>
<hr>
<div id="response"></div>
</body>
</html>