1html页面


2css样式
*{padding: 0;margin: 0;}
.container{
width: 1200px;
margin: 100px auto;
display: flex;
flex-wrap: wrap;
}
.container .product-item{
width: 260px;
height: 440px;
background-color: rgb(242,246,244);
margin-right: 35px;
}
.container .product-item img{
width: 100%;
}
通过js文件获取后端数据
使用ajax
function getProductList(){
let xhr = new XMLHttpRequest()
xhr.open('get','http://本机IP地址:8888/goods/list')
xhr.send()
xhr.onreadystatechange = function(){
if(xhr.readyState === 4){
if(xhr.status === 200){
let res = xhr.responseText
res = JSON.parse(res)
console.log('res ',res)
showProductList(res.list)
}
}
}
}
function showProductList(productList){
let list = productList.map(item=>{
return `<div class="product-item">
<img src="${item.img_big_logo}" alt="pic1">
<p>${item.title}</p>
<p>¥${item.price} ${item.goods_number}人已买</p>
</div>`
})
const rootEle = document.querySelector('.container')
rootEle.innerHTML = list.join('')
}
getProductList()