Blogger Information
Blog 43
fans 1
comment 0
visits 33478
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
Ajax跨域请求与无刷新分页技术
蔚蓝世纪
Original
768 people have browsed it

一、Ajax跨域请求分为以下7种

  1. load()请求数据
  2. $.get()请求数据
  3. $.post()请求数据
  4. $.getJSON()请求JSON数据
  5. $.ajax()请求数据
  6. $.ajax()-jsonp-跨域请求数据
  7. $.ajax()-jsonp-跨域请求数据

代码演示:

  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. <script src="lib/jquery-3.5.1.js"></script>
  7. <title>Ajax</title>
  8. <style>
  9. body {
  10. width: 200px;
  11. display: grid;
  12. gap: 15px;
  13. }
  14. button {
  15. text-align: left;
  16. height: 26px;
  17. }
  18. button.hover {
  19. background-color: #ddd;
  20. cursor: pointer;
  21. }
  22. </style>
  23. </head>
  24. <body>
  25. <button type="button">1.load()请求数据</button>
  26. <button type="button">2.$.get()请求数据</button>
  27. <button type="button">3.$.post()请求数据</button>
  28. <button type="button">4.$.getJSON()请求JSON数据</button>
  29. <button type="button">5.$.ajax()请求数据</button>
  30. <button type="button">6.$.ajax()-jsonp-跨域请求数据1</button>
  31. <button type="button">7.$.ajax()-jsonp-跨域请求数据2</button>
  32. </body>
  33. <script>
  34. // 1.load()请求数据 :加载html片断
  35. $("button:first-of-type").click(function () {
  36. // console.log($(this));
  37. $(this).after("<div>").next().load("nav.html");
  38. });
  39. //2.$.get():以get方式从服务器获取资源/数据
  40. $("button:nth-of-type(2)").click(function (ev) {
  41. // console.log($(this));
  42. // $.get(url,data,callback)
  43. $.get("users.php", { id: 2 }, function (data) {
  44. // console.log(data);
  45. $(ev.target).after("<div>").next().html(data);
  46. });
  47. });
  48. //3.$.post():以post方式从服务器获取资源/数据
  49. $("button:nth-of-type(3)").click(function (ev) {
  50. // console.log($(this));
  51. // $.post(url,data,callback)
  52. $.post("users.php", { id: 4 }, function (data) {
  53. // console.log(data);
  54. $(ev.target).after("<div>").next().html(data);
  55. });
  56. });
  57. //4.$.getJSON():接口返回的大多是JSON
  58. $("button:nth-of-type(3)").click(function (ev) {
  59. // console.log($(this));
  60. // $.get(url,data,callback)
  61. $.getJSON("users.php?id=2", function (data) {
  62. //返回的就是JS对象,不必再转换
  63. //console.log(JSON,parse(data));
  64. console.log(data);
  65. var res = data.id + ":" + data.name + "," + data.age + "岁";
  66. $(ev.target).after("<div>").next().html(data);
  67. });
  68. });
  69. // 5. $.ajax(): 终级方法,该方法的参数是一个对象自变量,前面4种方法可以放弃,只需要掌握这一个方法,方便快捷。
  70. // $.ajax({
  71. // //请求类型
  72. // type: "GET",
  73. // //请求URL
  74. // url: url,
  75. // //发送的数据
  76. // data: data,
  77. // //期望服务器返回/响应的数据类型
  78. // dataType: "json",
  79. // //成功时的回调函数
  80. // success: callback,
  81. // });
  82. $("button:nth-of-type(5)").click(function (ev) {
  83. $.ajax({
  84. type: "GET",
  85. url: "users.php",
  86. data: { id: 3 },
  87. dataType: "html",
  88. success: function (data) {
  89. $(ev.target).after("<div>").next().html(data);
  90. },
  91. });
  92. });
  93. //6.$.ajax()-jsonp-1:跨域请求数据
  94. $("button:nth-of-type(6)").click(function (ev) {
  95. $.ajax({
  96. type: "GET",
  97. url: "http://php.edu/0522/test2.php?jsonp=?&id=1",
  98. dataType: "jsonp",
  99. success: function (data) {
  100. console.log(data);
  101. var data = JSON.parse(data);
  102. console.log(data);
  103. var data =
  104. "<p>" +
  105. data.title +
  106. "</p><p>" +
  107. data.user.name +
  108. ", 邮箱:" +
  109. data.user.email +
  110. "</p>";
  111. $(ev.target).after("<div>").next().html(data);
  112. },
  113. });
  114. });
  115. //7.$.ajax()-jsonp-2:跨域请求数据
  116. $("button:last-of-type").click(function (ev) {
  117. $.ajax({
  118. type: "GET",
  119. url: "http://php.edu/0522/test2.php?jsonp=?&id=2",
  120. dataType: "jsonp",
  121. jsonpCallback: "handle",
  122. });
  123. });
  124. function handle(data) {
  125. console.log(data);
  126. var data = JSON.parse(data);
  127. console.log(data);
  128. var data =
  129. "<p>" +
  130. data.title +
  131. "</p><p>" +
  132. data.user.name +
  133. ", 邮箱:" +
  134. data.user.email +
  135. "</p>";
  136. $("button:last-of-type").after("<div>").next().html(data);
  137. }
  138. </script>
  139. </html>

输出效果:

二、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. <script src="lib/jquery-3.5.1.js"></script>
  7. <title>Ajax无刷新分页技术</title>
  8. <style>
  9. table {
  10. border-collapse: collapse;
  11. border: 1px solid;
  12. text-align: center;
  13. margin: auto;
  14. width: 500px;
  15. }
  16. table caption {
  17. font-size: 1.2rem;
  18. margin-bottom: 10px;
  19. }
  20. th,
  21. td {
  22. border: 1px solid;
  23. padding: 5px;
  24. }
  25. thead tr:first-of-type {
  26. background-color: rgb(70, 233, 245);
  27. }
  28. p {
  29. text-align: center;
  30. }
  31. p a {
  32. text-decoration: none;
  33. border: 1px solid;
  34. padding: 0 8px;
  35. }
  36. .active {
  37. background-color: rgb(70, 233, 245);
  38. }
  39. </style>
  40. </head>
  41. <body>
  42. <table>
  43. <caption>
  44. 员工信息表
  45. </caption>
  46. <thead>
  47. <tr>
  48. <th>id</th>
  49. <th>姓名</th>
  50. <th>年龄</th>
  51. <th>性别</th>
  52. <th>职位</th>
  53. <th>手机号</th>
  54. </tr>
  55. </thead>
  56. <tbody></tbody>
  57. </table>
  58. <!-- 分页条 -->
  59. <p></p>
  60. </body>
  61. <script>
  62. // 默认是第1页
  63. var page = 1;
  64. // 默认显示第一页,用一个函数来实现显示
  65. getPageData(page);
  66. // 分页函数
  67. function getPageData(page) {
  68. // ajax无刷新分页
  69. $.ajax({
  70. type: "post",
  71. url: "page_data.php",
  72. data: { page: page },
  73. dataType: "json",
  74. success: show,
  75. });
  76. }
  77. // 数据显示函数
  78. function show(data) {
  79. // 1. 显示表格数据
  80. console.log(data);
  81. console.log(data.pages);
  82. console.log(data.staffs);
  83. var str = "";
  84. data.staffs.forEach(function (staff) {
  85. str += "<tr>";
  86. str += "<td>" + staff.id + "</td>";
  87. str += "<td>" + staff.name + "</td>";
  88. str += "<td>" + staff.age + "</td>";
  89. str += "<td>" + staff.sex + "</td>";
  90. str += "<td>" + staff.position + "</td>";
  91. str += "<td>" + staff.mobile + "</td>";
  92. str += "</tr>";
  93. });
  94. // $("tbody").append(str);
  95. $("tbody").html(str);
  96. // 2. 显示分页条
  97. var str = "";
  98. for (var i = 1; i <= data.pages; i++) {
  99. // $("<a>").attr("href", "").attr("data-index", i).html(i).appendTo("p");
  100. str += '<a href="" data-index=' + i + ">" + i + "</a>";
  101. }
  102. // 将页码添到分页条, 并将第一个设置为高亮
  103. $("p").html(str).find("a").first().addClass("active");
  104. //将页码添到分页条, 并将最后一个设置为高亮 $("p").html(str).find("a").last().addClass("active");
  105. // 将当前页码设置为高亮
  106. $("p")
  107. .html(str)
  108. .find("a")
  109. .eq(page - 1)
  110. .addClass("active");
  111. // 分页条的点击事件
  112. $("p a").click(function (ev) {
  113. // 禁用<a>的跳转默认行为
  114. ev.preventDefault();
  115. var page = $(this).attr("data-index");
  116. $("tbody").html("");
  117. getPageData(page);
  118. });
  119. }
  120. </script>
  121. </html>

输出效果:

三、总结
1.代码执行过程中,使用Chrome浏览器预览时Ajax请求出现“Method Not Allowed 405”错误,表格数据无法加载,而firefox浏览器显示正常。百度到的解决方法:
  打开Chrome快捷方式的属性中设置:
  右击Chrome浏览器快捷方式,选择“属性”,
  在“目标”中加上”—allow-file-access-from-files”,注意前面有个空格,
  重启Chrome浏览器便可。
2.写代码的过程中一定要添加注释,注释是后期进行数据维护的依据。

Correcting teacher:天蓬老师天蓬老师

Correction status:qualified

Teacher's comments:完成的很好, 一二阶段作业务必在6月10日前完成
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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!