- http.js
```javascript
var baseUrl = “http://192.168.4.18:3000/“
function ajax({
url,
method=”get”,
success
}){
var xhr = new XMLHttpRequest()
xhr.open(method,url=baseUrl+url,true)
xhr.send()
xhr.onreadystatechange = function(){
} }if(xhr.readyState == 4 && xhr.status == 200){
var res = JSON.parse(xhr.responseText)
success(res)
}
function httpAlbum(callback){
ajax({
url:”top/album”,
success:res =>{
callback(res)
}
})
}
function handleData(res){
var arr = res.albums.slice(0,3)
// console.log(arr);
arr.forEach(item =>{
var template = <div class="container">
<img src=${item.blurPicUrl} alt="">
<p class="title">${item.name}</p>
<p class="name">${item.artist.name}</p>
</div>
$(“#app”).append(template)
})
}
- **index.js**
```javascript
httpAlbum(handleData) // 调用
网易云.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script src="js/http.js"></script>
<script src="https://cdn.bootcss.com/jquery/3.4.1/jquery.js"></script>
<script src="index.js"></script>
<style>
*{
margin: 0;
padding: 0;
}
#app{
width: 700px;
margin: 0 auto;
margin-top: 40px;
display: flex;
}
.container{
width: 160px;
/* border: 1px solid #666; */
margin-right: 20px;
}
.container img{
width: 150px;
height: 150px;
}
.title{
font-size: 14px;
}
.name{
color: #666;
font-size: 12px;
}
</style>
</head>
<body>
<div id="app">
</div>
</body>
</html>