Code:
const url = "https://marje.no/Wordpress/wp-json/wc/store/products?_embed";
const product = document.querySelector(".container");
async function getproduct() {
const response = await fetch(url);
const results = await response.json();
for (let i = 0; i < results.length; i++) {
console.log(results[i]);
product.innerHTML += `<div class="product">
<div class="productImg">
<img class="img" src="/${results[i].images.src}"
alt="${results[i].images[.0.alt]}">
</div>
<div class="productInfo">
<h3 class="productName">${results[i].name}</h3><p>
$ ${results[i].prices.price} ,- </p>
<a href="${results[i].permalink}"
class="button">View More</a>
</div>
</div>`;
}
}
getproduct();
my webpage: http://127.0.0.1:5500/html/cms-ca/cms-ca.html
For a school assignment:
I am trying the fetch data from a WordPress REST API web hosted page, and display that data on a separate page.
I am getting all the data I need, but when I try to pull the image inn, the path isn't recognised.
When I:
console.log(results[i]);
I get:
{
"id": 185,
"name": "Tulips",
"permalink": "https://marje.no/Wordpress/product/tulips/",
... other properties ...
"images": [{
"id": 186,
"src": "https://marje.no/Wordpress/wp-content/uploads/2021/03/tulips-scaled.jpg",
"thumbnail": "https://marje.no/Wordpress/wp-content/uploads/2021/03/tulips-324x324.jpg",
"srcset": "",
"sizes": "(max-width: 3648px) 100vw, 3648px",
"name": "tulips",
"alt": "colorful tulips"
}]
}
Sorry for the poor terminology, new to javascript!
Thanks for the help.