Blogger Information
Blog 36
fans 0
comment 0
visits 28107
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
Ajax方法实例
phpcn_u202398
Original
761 people have browsed it

1、Ajax方法

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <title>Ajax</title>
  7. <script src="jquery-3.4.1.js"></script>
  8. <style>
  9. .body{
  10. display: grid;
  11. gap: 15px;
  12. }
  13. .body > .button{
  14. width: 100px;
  15. height: 50px;
  16. font-size: 15px;
  17. }
  18. </style>
  19. </head>
  20. <body>
  21. <button type="button">load()请求数据</button>
  22. <button type="button">$.get()</button>
  23. <button type="button">$.post()</button>
  24. <button type="button">$.getJSON()请求JSON数据</button>
  25. <button type="button">$.get()</button>
  26. <button type="button">6. $.ajax()-jsonp-跨域请求数据1</button>
  27. <button type="button">7. $.ajax()-jsonp-跨域请求数据2</button>
  28. </body>
  29. </html>
  30. <script>
  31. // 1. load(): 加载html片断
  32. $("button:first-of-type").click(function () {
  33. $(this).after("<div>").next().load("nav.html");
  34. });
  35. //2.get():以get方式从服务器获取数据
  36. $("button:nth-of-type(2)").click(function (ev) {
  37. $.get("users.php", { id: 2 }, function (data) {
  38. $(ev.target).after("<div>").next().html(data);
  39. });
  40. });
  41. //3.post():以post方式从服务器获取数据
  42. $("button:nth-of-type(3)").click(function (ev) {
  43. $.post("users.php", { id: 2 }, function (data) {
  44. $(ev.target).after("<div>").next().html(data);
  45. });
  46. });
  47. // 4. $.getJSON():接口返回的大多是JSON
  48. $("button:nth-of-type(4)").click(function (ev) {
  49. $.getJSON("users.php?id=2", function (data) {
  50. var res = data.id + ": " + data.name + ", " + data.age + "岁";
  51. $(ev.target).after("<div>").next().html(res);
  52. });
  53. });
  54. // 5. $.ajax(): 终级方法, 实际上大家只需要掌握这一个方法
  55. $("button:nth-of-type(5)").click(function (ev) {
  56. $.ajax({
  57. type: "GET",
  58. url: "users.php",
  59. data: { id: 2 },
  60. dataType: "html",
  61. success: function (data) {
  62. $(ev.target).after("<div>").next().html(data);
  63. },
  64. });
  65. });
  66. // 6. $.ajax()-jsonp-1:跨域请求
  67. $("button:nth-of-type(6)").click(function (ev) {
  68. $.ajax({
  69. type: "GET",
  70. url: "http://php11.demo/0527/test.php?jsonp=?&id=2",
  71. dataType: "jsonp",
  72. success: function (data) {
  73. cl(data);
  74. var data = JSON.parse(data);
  75. cl(data);
  76. var data =
  77. "<p>" +
  78. data.title +
  79. "</p><p>" +
  80. data.user.name +
  81. ", 邮箱:" +
  82. data.user.email +
  83. "</p>";
  84. $(ev.target).after("<div>").next().html(data);
  85. },
  86. });
  87. });
  88. // 7. $.ajax()-jsonp-2:跨域请求
  89. $("button:last-of-type").click(function (ev) {
  90. $.ajax({
  91. type: "GET",
  92. url: "http://php11.demo/0527/test.php?jsonp=?&id=2",
  93. dataType: "jsonp",
  94. jsonpCallback: "handle",
  95. });
  96. });
  97. function handle(data) {
  98. cl(data);
  99. var data = JSON.parse(data);
  100. cl(data);
  101. var data =
  102. "<p>" +
  103. data.title +
  104. "</p><p>" +
  105. data.user.name +
  106. ", 邮箱:" +
  107. data.user.email +
  108. "</p>";
  109. $("button:last-of-type").after("<div>").next().html(data);
  110. }
  111. </script>

学习总结

本节课我们学习了Ajax方法,通过本节课的学习学到了Ajax方法,知道通过什么方式进行数据请求。有助于后期的实战应用。

Correcting teacher:天蓬老师天蓬老师

Correction status:qualified

Teacher's comments:异步编程对js来说是致命的技术, 一定要掌握
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post