我必須向以下 API 發送 AJAX 請求:
<!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>
我是否以錯誤的方式訪問資料?請指教,提前謝謝
uj5u.com熱心網友回復:
回應是一個物件陣列,如果要直接渲染,則需要獲取標題的字串值。
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>
uj5u.com熱心網友回復:
在 forEach 回圈中,${titles} 應該是 ${titles.title}
uj5u.com熱心網友回復:
像這樣做。
閱讀檔案以了解更多關于 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>
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/462393.html
標籤:javascript html 目的 句法 安慰
上一篇:jQuery.Swiper.js分頁不起作用,如何設定?
下一篇:桌面影像未在瀏覽器上加載
