Response: json() method
The json()
method of the Response
interface takes a Response
stream and reads it to completion. It returns a promise which resolves with the result of parsing the body text as JSON
.
const myList = document.querySelector( "ul" ); const myRequest = new Request( "products.json" ); fetch(myRequest) .then((response) => response.json()) .then((data) => { for (const product of data.products) { const listItem = document.createElement( "li" ); listItem.appendChild(document.createElement( "strong" )).textContent = product.Name; listItem.append(` can be found in ${product.Location}. Cost: `); listItem.appendChild(document.createElement( "strong" )).textContent = `£${product.Price}`; myList.appendChild(listItem); } }) . catch (console.error); |