【发布时间】:2019-05-28 00:44:19
【问题描述】:
我正在尝试使用 javascript 构建一个 API,以从该 URL 获取 json 数据的数据:img, a, c。但是下面的代码抛出了一个错误
application.js:10 Uncaught (in promise) TypeError:
data.forEachis not a function
(为什么没有定义forEach 方法)你能帮忙吗?谢谢
var results = document.getElementById("results");
fetch("https://www.mangaeden.com/api/list/0/")
.then(response => response.json())
.then((data) => {
data.forEach((result) => {
const movies = '<li><img src="' + result.im + '" alt=""><h3>' + result.a + '</h3><p>' + result.c + '</p></li>';
results.insertAdjacentHTML("beforeend", movies);
});
});
{
"a": "shoujo-apocalypse-adventure",
"c": [
"Adventure",
"Drama",
"Psychological",
"Sci-fi",
"Seinen",
"Slice of Life",
"Tragedy"
],
"h": 156,
"i": "5c410d31719a16035a4647cc",
"im": "4a/4a1f2a595e0e84e62f6ceddf3946274478928ca99e8df86bc6511b6e.png",
"ld": 1547822837.0,
"s": 2,
"t": "Shoujo Apocalypse Adventure"
},
【问题讨论】:
-
因为你的数据是一个对象,而不是一个数组。
-
这是响应吗?当我检查时,它的响应如下:
{ "end": -1, "manga": [ { "a": "joshiraku", "c": [ "Comedy", "Shounen", "Slice of Life" ], "h": 0, "i": "5bfdd0ff719a162b3c196677", "im": "4e/4e55aeda6ba2044eb2762124688b61e74f24880515e71827f1f1e2c4.png", "ld": 1543389646.0, "s": 2, "t": "Joshiraku" }, ]} } -
@jonrsharpe 最后的
,可能表明它是数组的一个元素。但是在这种情况下,说它是“Json 文件:”并不完全正确。 -
@tevemadar 如果它是一个数组,你就不会得到那个错误
-
@MoshFeu 是的!这是响应。当我做了简单的提取但没有显示时。
标签: javascript api fetch