example14.html 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="UTF-8">
  5. <title></title>
  6. <style>
  7. * {
  8. margin: 0;
  9. padding: 0;
  10. }
  11. #container {
  12. width: 960px;
  13. margin: 0 auto;
  14. }
  15. </style>
  16. </head>
  17. <body>
  18. <button id="load">加载</button>
  19. <div id="container"></div>
  20. <script src="https://cdn.bootcss.com/jquery/3.3.1/jquery.min.js"></script>
  21. <script>
  22. $(function() {
  23. $("#load").on("click", function() {
  24. console.log(encodeURIComponent("手机", "utf-8"));
  25. console.log(decodeURIComponent("%E7%8B%97%E5%B1%8E", "utf-8"));
  26. // 通过Ajax请求获得数据并对页面进行局部刷新
  27. // jQuery封装了多个Ajax请求方法:
  28. // - $.ajax(): 灵活强大(强烈推荐使用)
  29. // - $.getJSON(): 简单好用
  30. // 统一资源定位符
  31. // 协议://IP地址或域名:端口号/路径/资源?查询字符串
  32. // HTTP(s)协议的请求有多种请求命令
  33. // 浏览器在正常情况下只能发出get或post请求
  34. // 将来我们在项目中可能用到的HTTP请求命令包括以下5个:
  35. // - GET: 从服务器获取资源
  36. // - POST: 向服务器提交资源
  37. // - DELETE: 从服务器删除资源
  38. // - PUT / PATCH: 更新服务器上的资源
  39. var url = "http://api.tianapi.com/meinv/";
  40. $.ajax({
  41. "url": url,
  42. "type": "get",
  43. "data": {
  44. "key": "772a81a51ae5c780251b1f98ea431b84",
  45. "num": 15
  46. },
  47. "dataType": "json",
  48. "success": function(json) {
  49. for (var i = 0; i < json.newslist.length; i += 1) {
  50. var mm = json.newslist[i];
  51. $img = $("<img>").attr('src', mm.picUrl);
  52. $("#container").append($img.width(300));
  53. }
  54. }
  55. });
  56. });
  57. });
  58. </script>
  59. </body>
  60. </html>