0

I am trying to display the information from the API on the webpage but I can only see it on the console and am unsure why. Any help would be appreciated.

<!DOCTYPE html>
<html lang="en">
    <head>
        <title>Title</title>
        <meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
    </head>

    <body>  
    
        <div class="nutritionContainer">
            <div id="name"</div>
            <div id="calories"</div>
            
    <script src="nutrition.js" type="module"></script>
    </body>
</html>



fetch("https://nutrition-by-api-ninjas.p.rapidapi.com/v1/nutrition?query=coffee", {
    "method": "GET",
    "headers": {
        "x-rapidapi-host": "nutrition-by-api-ninjas.p.rapidapi.com",
        "x-rapidapi-key": "my_key"
    }
})
.then(response => response.json())
.then(response => {
    console.log(response);
    
    document.getElementById('name').innerHTML = response.content;
    document.getElementById('calories').innerHTML = response.content;

})
.catch(err => {
    console.log(err);
});
1
  • Is what you show how your page is actually written? That is, is that javascript really where you show it? Also, note that the <meta> tag does not use and does not need a closing slash and never has in any HTML specification. Commented Oct 10, 2021 at 23:40

1 Answer 1

1

The response is an array of object.

enter image description here

Suppose you want to print calories, you can do it something like this

document.getElementById('calories').innerHTML = response.data[0].calories;
Sign up to request clarification or add additional context in comments.

1 Comment

Please replace the image with the actual code.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.