1、Ajax的概念

Ajax就是从服务器获取数据的一种技术—管道
异步 涉及到对资源(文件的上传下载)的操作都应该是异步
http

  1. <script>
  2. /* Ajax就是从服务器获取数据的一种技术--管道 */
  3. /* 1 */
  4. console.log(1)
  5. con.log()
  6. console.log(2)
  7. /* 异步 涉及到对资源(文件的上传下载)的操作都应该是异步
  8. http
  9. */
  10. </script>

image.png

2、模拟异步操作

  1. <script>
  2. console.log(1);
  3. setTimeout(()=>{
  4. console.log("http")
  5. },1000)
  6. console.log(2)
  7. </script>

image.png

3、原生Ajax实现

  1. <script>
  2. /* 实现Ajax的前提
  3. 1、html页面
  4. 2、需要后端给我们提供的数据接口
  5. 3、DOM将获取的数据放到页面上
  6. */
  7. var url = "http://127.0.0.1:5000/mock/index.json"
  8. /* 1、创建Ajax的核心对象 */
  9. var xhr =new XMLHttpRequest();
  10. /* 2、需要与服务器建立连接 */
  11. xhr.open("GET",url,true)
  12. /* 3、发送请求 */
  13. xhr.send();
  14. /* 4、响应数据 */
  15. /* onreadystatechange监听服务器状态的变化 */
  16. xhr.onreadystatechange = function(){
  17. /* readyState值代表服务器响应的变化 ==4 请求响应完成 */
  18. /* status==200请求成功,数据成功的响应 */
  19. if(xhr.readyState == 4 && xhr.status == 200){
  20. console.log(xhr.responseText)
  21. var res = JSON.parse(xhr.responseText);
  22. console.log(res);
  23. }
  24. }
  25. </script>

image.png

4、JQuery实现Ajax

  1. <script>
  2. var url = "http://127.0.0.1:5000/mock/index.json"
  3. $.ajax({
  4. url,
  5. success:res=>{
  6. console.log(res);
  7. }
  8. })
  9. </script>

image.png

5、豆瓣接口实现

  1. <script>
  2. /* 前后端接口联调 */
  3. var url = "http://47.108.197.28:8000/book"
  4. var xhr = new XMLHttpRequest();
  5. xhr.open("get",url,true);
  6. xhr.send();
  7. xhr.onreadystatechange = function(){
  8. if(xhr.readyState==4 && xhr.status==200){
  9. var res = JSON.parse(xhr.responseText);
  10. console.log(res);
  11. }
  12. }
  13. </script>

image.png

  1. <script>
  2. var url = "http://47.108.197.28:8000/book";
  3. $.ajax({
  4. url,
  5. success:res=>{
  6. console.log(res);
  7. }
  8. })
  9. </script>

image.png

6、封装原生Ajax

  1. <script>
  2. var url = "http://47.108.197.28:3000/banner"
  3. function $ajax(url,success){
  4. var xhr = new XMLHttpRequest();
  5. xhr.open("get",url,true)
  6. xhr.send();
  7. xhr.onreadystatechange = function(){
  8. if(xhr.readyState==4 && xhr.status==200){
  9. var res = JSON.parse(xhr.responseText);
  10. success(res);
  11. }
  12. }
  13. }
  14. $ajax(url,res=>{{
  15. console.log(res);
  16. }})
  17. </script>

image.png
回调函数:

  1. <script>
  2. function $ajax(callback){
  3. var res = "hello world";
  4. callback(res);
  5. }
  6. $ajax(res=>{
  7. console.log(res)
  8. })
  9. </script>

image.png

  1. <script>
  2. function $ajax({
  3. success
  4. }){
  5. var res = "hello world"
  6. success(res);
  7. }
  8. $ajax({
  9. success:res=>{
  10. console.log(res);
  11. }
  12. })
  13. </script>

image.png

7、传参顺序

  1. <script>
  2. /* 函数的参数 */
  3. // function ajax(url,method,c){
  4. // console.log(url);
  5. // console.log(method);
  6. // console.log(c);
  7. // }
  8. // ajax("http","get",1)
  9. // ajax(1,"http","get")
  10. /* 如果参数过多,可以传对象 */
  11. function ajax({
  12. url,
  13. method,
  14. c
  15. }){
  16. console.log(url);
  17. console.log(method);
  18. console.log(c);
  19. }
  20. ajax({
  21. method:"get"
  22. })
  23. </script>

image.png

例子

1.

  1. <script>
  2. var url = "http://47.108.197.28:8000/book"
  3. function $ajax(url){
  4. var xhr = new XMLHttpRequest();
  5. xhr.open("get",url,true)
  6. xhr.send();
  7. xhr.onreadystatechange = function(){
  8. if(xhr.readyState==4 && xhr.status==200){
  9. var res = JSON.parse(xhr.responseText);
  10. console.log(res);
  11. }
  12. }
  13. }
  14. $ajax(url)
  15. </script>

image.png

2.

  1. <style>
  2. #container{
  3. width: 960px;
  4. border: 1px solid #333;
  5. overflow: hidden;
  6. }
  7. .item{
  8. width: 200px;
  9. /* height: 200px; */
  10. float: left;
  11. border: 1px solid #333;
  12. }
  13. .item img{
  14. width: 200px;
  15. }
  16. </style>
  17. </head>
  18. <body>
  19. <div id="container">
  20. <div class="item">
  21. <img src="" alt="">
  22. <p>hello</p>
  23. <p>hello</p>
  24. </div>
  25. </div>
  26. <script>
  27. url = "http://47.108.197.28:3000/top/playlist?order=hot"
  28. var xhr = new XMLHttpRequest();
  29. xhr.open("get",url,true)
  30. xhr.send();
  31. xhr.onreadystatechange = function(){
  32. if(xhr.readyState==4 && xhr.status==200){
  33. var res = JSON.parse(xhr.responseText);
  34. console.log(res.playlists);
  35. var playlists = res.playlists;
  36. var musics = [];
  37. playlists.forEach(item=>{
  38. var {name,coverImgUrl,playCount} = item;
  39. musics.push({
  40. name,
  41. coverImgUrl,
  42. playCount
  43. })
  44. })
  45. musics.splice(0,8).forEach(item=>{
  46. var {name,coverImgUrl,playCount} = item;
  47. var template = `
  48. <div class="item">
  49. <img src="${coverImgUrl}" alt="">
  50. <p>${name}</p>
  51. <p>${playCount}</p>
  52. </div>
  53. `
  54. $("#container").append(template)
  55. })
  56. }
  57. }
  58. </script>
  59. </body>

image.png
image.png

3.

  1. <script>
  2. var url = "http://47.108.197.28:3000/top/playlist?order=hot"
  3. $.ajax({
  4. url,
  5. success:res=>{
  6. console.log(res)
  7. }
  8. })
  9. </script>

image.png

4.

  1. <script>
  2. var ajax = function({url,success}){
  3. var xhr = new XMLHttpRequest();
  4. xhr.open("Get",url,true)
  5. xhr.send();
  6. xhr.onreadystatechange = function(){
  7. if(xhr.readyState==4 && xhr.status==200){
  8. var res = JSON.parse(xhr.responseText);
  9. success(res);
  10. }
  11. }
  12. }
  13. var url = "http://47.108.197.28:3000/top/album"
  14. ajax({
  15. url,
  16. success:res=>{
  17. console.log(res)
  18. }
  19. })
  20. </script>

image.png