Blogger Information
Blog 18
fans 1
comment 0
visits 17181
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
jquery的ajax常用方法
α清尘
Original
780 people have browsed it

jquery的ajax常用方法

ajax() 方法用于执行 AJAX(异步 HTTP)请求。
所有的 jQuery AJAX 方法都使用 ajax() 方法。该方法通常用于其他方法不能完成的请求。


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

jQuery ajax()方法 语法:

  1. $.ajax({name:value, name:value, ... })

部分代码演示:

  1. <script>
  2. // 1. load(): 请求数据, Html代码片断
  3. $("button:first-of-type").click(function(){
  4. $(this).after("<div>").next().load("ym.html");
  5. });
  6. // 2. $.get(): 幂操作
  7. $("button:nth-of-type(2)").click(function(ev){
  8. $.get("users.php",{id:2},function (data){
  9. console.log(data);
  10. $(ev.target).after("<div>").next().html(data);
  11. });
  12. });
  13. // 3. $.post():
  14. $("button:nth-of-type(3)").click(function(ev){
  15. $.post("users.php",{id:1},function(data){
  16. console.log(data)
  17. $(ev.target).after("<div>").next().html(data);
  18. })
  19. })
  20. $("button:nth-of-type(4)").click(function (ev) {
  21. $.getJSON("users.php?id=3", function (data) {
  22. // 从服务器返回josn会自动解析为JS对象,JSON.parse()
  23. console.log(data);
  24. data = `${data.id}: ${data.name}, 年龄: ${data.age}`;
  25. $(ev.target).after("<div>").next().html(data);
  26. });
  27. });
  28. $("button:nth-of-type(5)").click(function (ev) {
  29. $.ajax({
  30. // 请求类型
  31. type: "GET",
  32. // 请求URL
  33. url: "users.php",
  34. // 发送的数据
  35. data: { id: 1 },
  36. // 希望服务器返回的数据类型
  37. dataType: "html",
  38. // 成功的回调处理方法
  39. success(data) {
  40. $(ev.target).after("<div>").next().html(data);
  41. },
  42. });
  43. });
  44. // 6. $.ajax()-jsonp: 跨域请求数据1
  45. $("button:nth-of-type(6)").click(function (ev) {
  46. $.ajax({
  47. type: "GET",
  48. // jsonp=?, ?是回调方法的占位符,请求发送时用jsonpCallback替换
  49. url: "http://php.io/test.php?id=2&jsonp=?",
  50. dataType: "jsonp",
  51. jsonpCallback: "handle",
  52. });
  53. });
  54. function handle(data) {
  55. console.log(data);
  56. data = `Name: ${data.name}, Email: ${data.email}`;
  57. $("button:nth-of-type(6)").after("<div>").next().html(data);
  58. }
  59. // 7. $.ajax()-jsonp: 跨域请求数据2
  60. $("button:last-of-type").click(function (ev) {
  61. $.ajax({
  62. type: "GET",
  63. // jsonp=?, ?是回调方法的占位符,请求发送时用jsonpCallback替换
  64. url: "http://php.io/test.php?id=3&jsonp=?",
  65. dataType: "jsonp",
  66. success(data) {
  67. console.log(data);
  68. data = `Name: ${data.name}, Email: ${data.email}`;
  69. $("button:last-of-type").after("<div>").next().html(data);
  70. },
  71. });
  72. });
  73. </script>

效果展示

$ajax(){}步骤:

//设置请求类型 type:”get/post”,
//设置请求地址 url: “url地址” ,
//发送数据 数据保存在date里面 date值是查询条件 date:” {id:1}” ,
//设置服务器希望返回的数据类型 datetype:” html/json/…”,
//请求成功后的回调函数 succes( 设置函数的参数 ){ 设置函数 }

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!