[目前,我正在尝试学习如何从API访问数据并将其放入html的div中。我正在使用访存,但是我不确定在对数据进行字符串化后应该去哪里。我想从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>