我将如何创建一个div并将JSON中的'brands'数据放入其中?

问题描述 投票:0回答:1

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

fetch("http://makeup-api.herokuapp.com/api/v1/products.json")
    .then(res => res.json())
    .then(
    (data) => {
        JSON.stringify(data)
    }
javascript json api fetch-api
1个回答
0
投票

我建议使用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>
© www.soinside.com 2019 - 2024. All rights reserved.