回调地狱:多层回调函数嵌套

例子:网易云音乐

  1. <script src="https://cdn.bootcss.com/jquery/3.4.1/jquery.js"></script>
  1. var baseUrl="https://music.aityp.com/";
  2. $.ajax({
  3. url:baseUrl+"top/playlist",
  4. type:"get",
  5. data:{
  6. cat:"华语"
  7. },
  8. dataType:"json",
  9. success:res=>{
  10. var {playlists}=res;
  11. var item=playlists[0];
  12. console.log(item)
  13. $.ajax({
  14. url:`${baseUrl}playlist/detail?id=${item.id}`,
  15. type:"get",
  16. success:res=>{
  17. let id=res.playlist.trackIds[0].id;
  18. console.log(id)
  19. $.ajax({
  20. url:`${baseUrl}song/url?id=${id}`,
  21. success:res=>{
  22. console.log(res)
  23. }
  24. })
  25. }
  26. })
  27. }
  28. })

解决回调地狱的方法

1.Promise()