Dz12js.html 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7. <title>Document</title>
  8. </head>
  9. <body>
  10. <script>
  11. function fetchBasic() {
  12. fetch('https://swapi.dev/api/people/1/')
  13. .then((res) => res.json())
  14. .then((luke) => {
  15. basic(document.body, luke)
  16. });
  17. function basic(dom, json) {
  18. const table = document.createElement("table");
  19. table.setAttribute("border", "2")
  20. dom.appendChild(table);
  21. for (let key in json) {
  22. const tr = document.createElement("tr")
  23. tr.setAttribute("style", "background:#00FA9A")
  24. const th = document.createElement("th");
  25. th.setAttribute("style", "background:#40E0D0")
  26. const td = document.createElement("td");
  27. td.setAttribute("style", "background:#9400D3")
  28. th.innerText = key;
  29. td.innerText = json[key];
  30. tr.appendChild(th);
  31. tr.appendChild(td);
  32. table.appendChild(tr)
  33. }
  34. }
  35. }
  36. // fetchBasic()
  37. function fetchImproved() {
  38. fetch('https://swapi.dev/api/people/1/')
  39. .then((res) => res.json())
  40. .then((luke) => {
  41. improved(luke)
  42. });
  43. function improved(json) {
  44. const table = document.createElement("table");
  45. table.setAttribute("border", "2")
  46. for (let [key, value] of Object.entries(json)) {
  47. const URL = "https://swapi.co/api/";
  48. const tr = document.createElement("tr");
  49. tr.setAttribute("style", "background:#00FA9A")
  50. const td = document.createElement("td");
  51. td.setAttribute("style", "background:#9400D3")
  52. const th = document.createElement("th");
  53. th.setAttribute("style", "background:#40E0D0")
  54. document.body.appendChild(table)
  55. table.appendChild(tr)
  56. tr.appendChild(th)
  57. tr.appendChild(td);
  58. th.innerHTML = key;
  59. td.innerHTML = value
  60. if (!String(value).indexOf(URL)) {
  61. const button = document.createElement("button")
  62. button.innerHTML = value;
  63. button.setAttribute("style", "background: #FF4500")
  64. td.appendChild(button);
  65. }
  66. if (typeof value === "object") {
  67. td.innerHTML = '';
  68. for (let key in value) {
  69. const button = document.createElement("button")
  70. td.appendChild(button);
  71. button.setAttribute("style", "background:#FF8C00")
  72. button.innerHTML = value[key];
  73. button.onclick = () => {
  74. document.body.removeChild(table)
  75. fetch(value[key])
  76. .then((res) => res.json())
  77. .then((json) => improved(json))
  78. }
  79. }
  80. }
  81. }
  82. }
  83. }
  84. // //fetchImproved()
  85. function myfetch(url) {
  86. return new Promise(function (resolve, reject) {
  87. const xhr = new XMLHttpRequest();
  88. xhr.open('GET', url, true);
  89. xhr.responseType = "json";
  90. xhr.onload = function () {
  91. if (xhr.status == 200) {
  92. resolve(xhr.response);
  93. }
  94. else {
  95. reject(`ERROR ${xhr.status} - ${xhr.statusText}`);
  96. }
  97. }
  98. xhr.onerror = () => reject(`ERROR ${xhr.status} - ${xhr.statusText}`);
  99. xhr.send();
  100. })
  101. }
  102. // race
  103. </script>
  104. </body>
  105. </html>