Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Fetching data from API using Javascript and then pushing the value to a DIV

I am trying to get data from a free APi : API URL

I then want to push the full response into a div on my page.
I can consolelog it fine, my solution is not working.
This is what i have tried.

async function fetchData() {
    fetch('https://jsonplaceholder.typicode.com/posts/1/comments')
  .then((response) => response.json())
  .then((json) => console.log(json));
    i = json;
    console.log(i);
    document.getElementById('matcher').innerHTML = i.toString();
}

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

async function fetchData() {
    fetch('https://jsonplaceholder.typicode.com/posts/1/comments').then(function (response) {
    // The API call was successful!
    return response.json();
}).then(function (data) {
    // This is the JSON from our response
    console.log(data);
    document.getElementById('matcher').innerHTML = data[0].body;
}).catch(function (err) {
    // There was an error
    console.warn('Something went wrong.', err);
});
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading