index.html 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>Title</title>
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7. <style>
  8. #app {
  9. margin: 20px;
  10. }
  11. </style>
  12. <script>
  13. </script>
  14. </head>
  15. <body>
  16. <div id="app">
  17. 加载中……
  18. </div>
  19. <script>
  20. function getQueryStringByUrl(url, name) {
  21. let reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
  22. let r = url.substring(url.indexOf('?') + 1).match(reg); //获取url中"?"符后的字符串并正则匹配
  23. let context = "";
  24. if (r != null)
  25. context = r[2];
  26. reg = null;
  27. r = null;
  28. return context == null || context === "" || context === "undefined" ? "" : decodeURI(context);
  29. }
  30. function getQueryString(name) {
  31. return getQueryStringByUrl(location.href, name);
  32. }
  33. const requestUtil = {
  34. xhr_send(xhr, method, headers, data) {
  35. if (headers) {
  36. for (const key in headers) {
  37. xhr.setRequestHeader(key, headers[key]);
  38. }
  39. }
  40. if (method.match(/^(POST|PUT|DELETE)$/i)) {
  41. if (!headers || !headers.hasOwnProperty("Content-Type")) {
  42. xhr.setRequestHeader("Content-Type", "application/json");
  43. }
  44. xhr.send(JSON.stringify(data));
  45. } else {
  46. xhr.send();
  47. }
  48. },
  49. /**
  50. * 异步请求:requestUtil.async('https://jsonplaceholder.typicode.com/posts/1', 'GET', null, null)
  51. * .then(data => console.log(data))
  52. * .catch(error => console.error(error));
  53. */
  54. async(url, method, data = {}, headers = {}) {
  55. return new Promise((resolve, reject) => {
  56. const xhr = new XMLHttpRequest();
  57. xhr.open(method, url, true);
  58. this.xhr_send(xhr, method, headers, data);
  59. xhr.onload = () => {
  60. resolve(JSON.parse(xhr.responseText));
  61. };
  62. xhr.onerror = () => reject(xhr.statusText);
  63. });
  64. },
  65. /**
  66. * 拼接 url
  67. */
  68. buildUrl(url, params) {
  69. const urlObj = new URL(url);
  70. // @ts-ignore
  71. for (const key in params) {
  72. urlObj.searchParams.set(key, params[key]);
  73. }
  74. return urlObj.toString();
  75. },
  76. /**
  77. * 同步请求 let a = request.sync("https://httpbin.tianyunperfect.cn/ip","GET",null,null)
  78. */
  79. sync(url, method, data = {}, headers = {}) {
  80. const xhr = new XMLHttpRequest();
  81. xhr.open(method, url, false);
  82. this.xhr_send(xhr, method, headers, data);
  83. return JSON.parse(xhr.responseText);
  84. },
  85. };
  86. let id = getQueryString("id");
  87. console.log(id)
  88. // 如果没有id参数,直接返回
  89. if (id) {
  90. requestUtil.async("https://app.yizhigj.com/api/content/getById?id=" + id, "GET",).then(res => {
  91. document.getElementById("app").innerHTML = res.data.content;
  92. });
  93. }
  94. </script>
  95. </body>
  96. </html>