PHP8.1.21版本已发布
vue8.1.21版本已发布
jquery8.1.21版本已发布

博客列表 > jquery的$.ajax常用方法大全,案例写成这般6!是什么级别啊...

jquery的$.ajax常用方法大全,案例写成这般6!是什么级别啊...

张福根一修品牌运营
张福根一修品牌运营 原创
2020年11月12日 16:05:26 605浏览

熟悉jquery的$.ajax方法

案例展示:

jquery的$.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>Document</title>
  7. <style>
  8. body {
  9. display: grid;
  10. gap: 15px;
  11. }
  12. button {
  13. padding: 0.5em;
  14. text-align: left;
  15. }
  16. button span {
  17. color: red;
  18. }
  19. button:hover {
  20. background-color: lightcyan;
  21. cursor: pointer;
  22. }
  23. </style>
  24. </head>
  25. <body>
  26. <button>1. load(): 请求数据</button>
  27. <button>2. $.get(): 请求数据</button>
  28. <button>3. $.post(): 请求数据</button>
  29. <button>4. $.getJSON(): 请求JSON数据</button>
  30. <button>5. $.ajax(): 请求数据</button>
  31. <button>6. $.ajax()-jsonp: 跨域请求数据1</button>
  32. <button>7. $.ajax()-jsonp: 跨域请求数据2</button>
  33. <script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.5.1/jquery.js"></script>
  34. <script>
  35. // 1. load(): 请求数据, 实际上就是一个Html代码片断
  36. $("button:first-of-type").click(function () {
  37. $(this).after("<div>").next().load("nav.html");
  38. });
  39. // 2. $.get(). 幂操作
  40. // get: users.php?id=1
  41. // $.get(url, callback)
  42. // $.get("users.php?id=1", function (data) {
  43. $("button:nth-of-type(2)").click(function (ev) {
  44. $.get("users.php", { id: 1 }, function (data) {
  45. console.log(data);
  46. $(ev.target).after("<div>").next().html(data);
  47. });
  48. });
  49. // 3. $.post()
  50. $("button:nth-of-type(3)").click(function (ev) {
  51. $.get("users.php", { id: 1 }, function (data) {
  52. console.log(data);
  53. $(ev.target).after("<div>").next().html(data);
  54. });
  55. });
  56. // 4. $.getJSON()
  57. $("button:nth-of-type(4)").click(function (ev) {
  58. $.getJSON("users.php?id=1", function (data) {
  59. console.log(data);
  60. data = `${data.id}:${data.name},年龄:${data.age}`;
  61. $(ev.target).after("<div>").next().html(data);
  62. });
  63. });
  64. // 5. $.ajax(): 请求数据
  65. $("button:nth-of-type(5)").click(function (ev) {
  66. $.ajax({
  67. type: "get",
  68. url: "users.php",
  69. data: { id: 1 },
  70. dataType: "html",
  71. success(data) {
  72. $("button:nth-of-type(5)").after("<div>").next().html(data);
  73. console.log(data);
  74. },
  75. });
  76. });
  77. // 6. $.ajax()-jsonp: 跨域请求数据1
  78. $("button:nth-of-type(6)").click(function (ev) {
  79. $.ajax({
  80. type: "get",
  81. // fugen=?, ?是回调方法的占位符,请求发送时用jsonpCallback替换
  82. url: "http://php.yu/test.php?id=2&fugen=?",
  83. dataType: "jsonp",
  84. jsonpCallback: "handle",
  85. });
  86. });
  87. function handle(data) {
  88. console.log(data);
  89. data = `姓名:${data.name},邮箱:${data.email}`;
  90. $("button:nth-of-type(6)").after("<div>").next().html(data);
  91. }
  92. // 7. $.ajax()-jsonp: 跨域请求数据2
  93. $("button:last-of-type").click(function (ev) {
  94. $.ajax({
  95. type: "GET",
  96. url: "http://php.yu/test.php?id=3&fugen=?",
  97. dataType: "jsonp",
  98. success(data) {
  99. console.log(data);
  100. data = `名称: ${data.name}, 邮箱: ${data.email}`;
  101. $(ev.target).after("<div>").next().html(data);
  102. },
  103. });
  104. });
  105. </script>
  106. </body>
  107. </html>

案例总结:

$.ajax() = $.load() + $.get() + $.post() + $.getJSON() + $.getScript()
$.ajax({
请求类型
type: “GET”,
请求的url地址
url: url,
发送数据
data: data,
希望服务器端响应返回的数据类型是什么
dataType: “json”,
请求成功的回调方法
success: callback,
});


$.ajax()-jsonp: 跨域请求数据
fugen=?, ?是回调方法的占位符,请求发送时用jsonpCallback替换

声明:本文内容转载自脚本之家,由网友自发贡献,版权归原作者所有,如您发现涉嫌抄袭侵权,请联系admin@php.cn 核实处理。
全部评论
文明上网理性发言,请遵守新闻评论服务协议