回调地狱:多层回调函数嵌套
例子:网易云音乐
<script src="https://cdn.bootcss.com/jquery/3.4.1/jquery.js"></script>
var baseUrl="https://music.aityp.com/";
$.ajax({
url:baseUrl+"top/playlist",
type:"get",
data:{
cat:"华语"
},
dataType:"json",
success:res=>{
var {playlists}=res;
var item=playlists[0];
console.log(item)
$.ajax({
url:`${baseUrl}playlist/detail?id=${item.id}`,
type:"get",
success:res=>{
let id=res.playlist.trackIds[0].id;
console.log(id)
$.ajax({
url:`${baseUrl}song/url?id=${id}`,
success:res=>{
console.log(res)
}
})
}
})
}
})
解决回调地狱的方法
1.Promise()