批改状态:合格
老师批语:
简化代码改成同步风格:
function getData(ele) {fetch('https://jsonplaceholder.typicode.com/todos/20').then(function (response) {return response.json()}).then(function(json) {console.log(json)//将返回的数据显示在li上ele.insertAdjacentHTML('afterend', `<li>${json.title}</li>`)})}
const url = 'https://jsonplaceholder.typicode.com/todos/20'fetch(url).then(res => res.json()).then(json => ele.insertAdjacentHTML('afterend', `<li>${json.title}</li>`)).catch(err => console.log(err))
async function getData(ele) {const url = 'https://jsonplaceholder.typicode.com/todos/15'try {//await 必须用在 async 声明的函数内部const response = await fetch(url)const result = await response.json()console.log(result)// 把title的值渲染到到页面ele.insertAdjacentHTML('afterend', `<li>${result.title}</li>`)} catch {console.error('请求失败')}}
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号