使用 fetch 的 API - 无法读取未定义的 属性 'forEach'
APIs using fetch - cannot read property 'forEach' of undefined
我正在尝试使用 API https://api.got.show/api/general/characters/ 从权力的游戏(节目)中提取所有角色名称的 select 。我尝试了多种方法,但都失败了。现在它显示 "TypeError: Cannot read property 'forEach' of undefined",当我尝试使用其他 API 并且它有效时。我不知道该怎么办。到目前为止,这是我的代码:
<body>
<h1>Game of thrones</h1>
<select>
<option id="characters"></option>
</select>
<script>
fetch('https://api.got.show/api/general/characters/')
.then(data => data.json())
.then(data_json => {
console.log(data_json)
show(data_json)
})
.catch(err => alert(`Ha habido un error. ${err}`))
let show = data_json => {
data_json.results.forEach((element, index) => {
document.getElementById('characters').innerHTML += `<option class""character">${element.name}</option>`;
});
}
</script>
</body>
我试着自己给 API 打电话。看起来 data_json
有两个属性 book
和 show
但没有 results
.
为 show
函数尝试此代码段:
let show = data_json => {
data_json.show.forEach((element, index) => {
document.getElementById('characters').innerHTML += `<option class="character">${element.name}</option>`;
});
}
我正在尝试使用 API https://api.got.show/api/general/characters/ 从权力的游戏(节目)中提取所有角色名称的 select 。我尝试了多种方法,但都失败了。现在它显示 "TypeError: Cannot read property 'forEach' of undefined",当我尝试使用其他 API 并且它有效时。我不知道该怎么办。到目前为止,这是我的代码:
<body>
<h1>Game of thrones</h1>
<select>
<option id="characters"></option>
</select>
<script>
fetch('https://api.got.show/api/general/characters/')
.then(data => data.json())
.then(data_json => {
console.log(data_json)
show(data_json)
})
.catch(err => alert(`Ha habido un error. ${err}`))
let show = data_json => {
data_json.results.forEach((element, index) => {
document.getElementById('characters').innerHTML += `<option class""character">${element.name}</option>`;
});
}
</script>
</body>
我试着自己给 API 打电话。看起来 data_json
有两个属性 book
和 show
但没有 results
.
为 show
函数尝试此代码段:
let show = data_json => {
data_json.show.forEach((element, index) => {
document.getElementById('characters').innerHTML += `<option class="character">${element.name}</option>`;
});
}