目前,我正在尝试学习如何从 API 访问数据并将其放入我的 html 的 div 中。我正在使用 fetch,但我不确定在对数据进行字符串化后该去哪里。我想从 API 访问“品牌”数据并将其放入我的 html 中。
fetch("http://makeup-api.herokuapp.com/api/v1/products.json")
.then(res => res.json())
.then(
(data) => {
JSON.stringify(data)
}
最佳答案
我建议使用this tutorial学习 JavaScript 获取。
这是一个工作片段(尽管它在 SO 中不起作用)。
const url = 'http://makeup-api.herokuapp.com/api/v1/products.json';
fetch(url)
.then((resp) => resp.json())
.then(function(data) {
let products = data;
return products.map(function(product) {
console.log(product.brand);
//$("#listOfBrands").append(product.brand); // do as you will
})
})
.catch(function(error) {
console.log(error);
});
<div id="listOfBrands"></div>
关于javascript - 我如何创建一个 div 并将 JSON 中的 'brands' 数据放入其中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58774281/