I'm trying to extract the title from my API using JavaScript and then display it in HTML.
api:(https://k0wa1un85b.execute-api.us-east-1.amazonaws.com/production/books)
The JSON response is as follows:
{"statusCode":200,"body":[{"id":"4f160b1f8693cdbeb96ac8be135ff0f9","title":"Harry Potter"}]}
This is the JavaScript code used:
<body>
<p>
title: <span id="tit"></span><br/>
</p>
<script>
const api_url = 'https://k0wa1un85b.execute-api.us-east-1.amazonaws.com/production/books';
async function getTitle() {
const response = await fetch(api_url);
const data = await response.json();
console.log(data);
const {title} = (data);
document.getElementById('tit').textContent = title;
}
getTitle();
</script>
</body