当前位置: 首页 > 知识库问答 >
问题:

如何使用JavaScipt将Fetch Data In设置为数组和将Data In设置为innerHTML Boostrap卡

海宁
2023-03-14

我有这样的方法。

async function sendApiRequest() {
    let APP_ID = "f61bb958";
    let APP_Key = "7c465e19d8e2cb3bc8f79e7a6e18961e"
    let INPUT_VALUE = document.getElementById("inputRecipe").value;
    console.log(INPUT_VALUE)
    fetch(`https://api.edamam.com/search?app_id=${APP_ID}&app_key=${APP_Key}&q=${INPUT_VALUE}`)
        .then(response => response.json())
        .then(data => {
            console.log(data);
            document.getElementById("ripeCard").innerHTML = `
       <div class="card" style="width: 18rem;">
           <img src="${data.image}" class="card-img-top" alt="...">
          <div class="card-body">
          <h5 class="card-title">Card title</h5>
           <p class="card-text">Some quick example text to build on the card title and make up the bulk of the card's content.</p>
        <a href="#" class="btn btn-primary">Go somewhere</a>         
      </div>
            `
        })
}

API返回数据如下:

count: 7000
from: 0
hits: Array(10)
0:
recipe: {uri: "http://www.edamam.com/ontologies/edamam.owl#recipe_1b6dfeaf0988f96b187c7c9bb69a14fa", label: "Pizza Dough", image: "https://www.edamam.com/web-img/284/2849b3eb3b46aa0e682572d48f86d487.jpg", source: "Lottie + Doof", url: "http://www.lottieanddoof.com/2010/01/pizza-pulp-fiction-jim-lahey/", …}
__proto__: Object
1:
recipe: {uri: "http://www.edamam.com/ontologies/edamam.owl#recipe_0209cb28fc05320434e2916988f47b71", label: "White Pizza", image: "https://www.edamam.com/web-img/dfe/dfe2e44c86334a3a3a5f774dda576121.jpg", source: "Food52", url: "https://food52.com/recipes/40095-white-pizza", …}
__proto__: Object
2:
recipe: {uri: "http://www.edamam.com/ontologies/edamam.owl#recipe_77db6e25dffe1836d4407cf4575f0141", label: "Chorizo, caper & rocket pizza", image: "https://www.edamam.com/web-img/7fe/7fee72cbf470edc0089493eb663a7a09.jpg", source: "BBC Good Food", url: "http://www.bbcgoodfood.com/recipes/1506638/chorizo-caper-and-rocket-pizza", …}
__proto__: Object
3:
recipe: {uri: "http://www.edamam.com/ontologies/edamam.owl#recipe_c9bf37296a0126d18781c952dc45a230", label: "Pizza Frizza recipes", image: "https://www.edamam.com/web-img/94a/94aeb549b29ac92dced2ac55765f38f9", source: "Martha Stewart", url: "http://www.marthastewart.com/284463/pizza-frizza", …}
__proto__: Object
4:
recipe: {uri: "http://www.edamam.com/ontologies/edamam.owl#recipe_b2ebad01df2a319d259c2d3f61eb40c5", label: "Margarita Pizza With Fresh Mozzarella & Basil", image: "https://www.edamam.com/web-img/d72/d72d1b7dd988e3b340a4b90ed3d56603.jpg", source: "Big Girls Small Kitchen", url: "http://www.biggirlssmallkitchen.com/2010/06/cooking-for-others-bff-pizza-party.html", …}
__proto__: Object
5:
recipe: {uri: "http://www.edamam.com/ontologies/edamam.owl#recipe_1b25671a32284038b57ad8b49c44af68", label: "Grilled BLT Pizza recipes", image: "https://www.edamam.com/web-img/2ac/2ac98d88117ff8f2327d302ab290b164", source: "Food Republic", url: "http://www.foodrepublic.com/recipes/grilled-blt-pizza-recipe/", …}
__proto__: Object
6: {recipe: {…}}
7: {recipe: {…}}
8: {recipe: {…}}
9: {recipe: {…}}
length: 10
__proto__: Array(0)
more: true
q: "pizza"
to: 10

并且我需要将Fetch Data设置为innerHTML Boostrap卡。但要做到这一点,我需要将fetch数据设置到数组中。我如何设置我的取数组,以及如何设置取数到innerHTML Boostrap卡..?

谢谢..!

共有1个答案

从智志
2023-03-14

我认为您需要API提供的是菜谱,因此API在hits属性中返回菜谱。code>hits属性是制作方法的数组

recipe对象中的属性为;所以你可以选择你需要的,

['uri', 'label', 'image', 'source', 'url', 'shareAs', 'yield', 'dietLabels', 'healthLabels', 'cautions', 'ingredientLines', 'ingredients', 'calories', 'totalWeight', 'totalTime', 'cuisineType', 'mealType', 'dishType', 'totalNutrients', 'totalDaily', 'digest'];

null

async function sendApiRequest() {
    let APP_ID = 'f61bb958';
    let APP_Key = '7c465e19d8e2cb3bc8f79e7a6e18961e';
    let INPUT_VALUE = document.getElementById('inputRecipe').value;
    const response = await fetch(`https://api.edamam.com/search?app_id=${APP_ID}&app_key=${APP_Key}&q=${INPUT_VALUE}`);
    const data = await response.json();

    document.getElementById('ripeCard').innerHTML = ''; // Resetting the content of the element

    // Iterating through the array of recipes and destructuring the result object to give only the recipe property
    data.hits.forEach(({recipe}) => {
        document.getElementById('ripeCard').innerHTML += `
       <div class="card" style="width: 18rem;">
           <img src="${recipe.image}" class="card-img-top" alt="...">
          <div class="card-body">
          <h5 class="card-title">${recipe.label}</h5>
           <p class="card-text">Some quick example text to build on the card title and make up the bulk of the card's content.</p>
        <a href="${recipe.url}" class="btn btn-primary">Go somewhere</a>         
      </div>`;
    });
}

sendApiRequest();
<input id="inputRecipe" value="pizza">
<div id="ripeCard"></div>
 类似资料:
  • 问题内容: 我正在尝试显示媒体存储的多个列,例如艺术家,专辑,标题等。我能够正确获取arraylist中的一个列,并且可以正常工作,但是当我添加另一个列时,所有列都在同一列中数组列表,所以我创建了一个类,并在该类方法中添加了列。但是我无法在custome适配器中声明它,这是我的代码。 活动主体 方法类别 CustomeAdapter 定制XML 如何将方法设置为数组。如何获取任何示例的方法都将有所

  • 我正在开发springboot应用程序,在该应用程序中,我需要为请求API添加一个标头,它们使用org.springframework.http.HttpHeaders将标头值设置为请求。 我可以看到他们使用下面的代码来设置字符串值标题。 https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframe

  • 问题内容: 我尝试过这种方式,但是它没有改变吗? 问题答案: 最好使用.png文件;.ico是Windows特定的。最好不要使用文件,而是使用类资源(可以包装在应用程序的jar中)。 尽管您甚至可能考虑将setIconImages用于多种尺寸的图标。

  • 我使用的是Spring Kafka 2.2.7,我已经将配置为,并使用消费消息,一切都按预期运行。 我想添加一个来记录所有消耗的消息,但发现很难配置它。留档指出可以在容器上设置RecordInterceptor,但我不确定如何获取容器的实例。 从版本2.2.7开始,您可以向侦听器容器添加RecordInterceptor;它将在调用允许检查或修改记录的侦听器之前被调用。 我查阅了Spring文档,

  • 我有一个设置活动在Android Studio,用户将在文本框中输入一个名称。他们将点击保存并进入另一个活动,他们的名字将显示在文本视图中。我在尝试在文本视图中显示名称时遇到了麻烦,我尝试了一些不同的方法,并得到了一些不同的错误,这可能是一些简单的东西,我没有得到或忽略。这是我的错误代码 我的错误是:

  • 问题内容: 更新 :对于最新的Angular版本,该问题已过时,请参阅tsh对此职位的评论 我已经将复选框绑定到一个值: 控制器中复选框的值设置为1: 但是,最初该复选框未显示为选中状态。如果我将的初始值更改为,则可以。(jsfiddle演示) 我尝试了各种变化: 他们都不工作。如何使angular将参数视为数字? 问题答案: 您可以使用 ngChecked ,如果表达式为真,则将在元素上设置特殊