|
10 | 10 | text-align: center; |
11 | 11 | background-color: whitesmoke; |
12 | 12 | } |
13 | | - button{ |
| 13 | + ul{ |
| 14 | + list-style: none; |
| 15 | + } |
| 16 | + img { |
| 17 | + width: 250px; |
| 18 | + height: 250px; |
| 19 | + } |
| 20 | + #axiosBtn, #xhrBtn{ |
14 | 21 | padding: 10px; |
15 | 22 | margin: 10px; |
16 | 23 | font-weight: 600; |
|
19 | 26 | </head> |
20 | 27 | <body> |
21 | 28 | <dvi> |
22 | | - <button id="btn">Get by Axios</button> |
23 | | - <button id="btn2">Get by XHR</button> |
| 29 | + <button id="axiosBtn">Get by Axios</button> |
| 30 | + <button id="xhrBtn">Get by XHR</button> |
24 | 31 | <div id="main"> |
25 | | - <ul> |
26 | | - <li> |
27 | | - |
28 | | - </li> |
29 | | - </ul> |
30 | 32 | </div> |
31 | 33 |
|
32 | 34 | </dvi> |
33 | 35 |
|
34 | 36 | <script src="https://unpkg.com/axios/dist/axios.min.js"></script> |
35 | 37 | <script> |
36 | 38 | let mainDiv = document.getElementById('main') |
37 | | - let getByAxios= document.getElementById('btn'); |
38 | | - let getByXHR = document.getElementById('btn2'); |
| 39 | + let getByAxios= document.getElementById('axiosBtn'); |
| 40 | + let getByXHR = document.getElementById('xhrBtn'); |
39 | 41 | getByAxios.addEventListener('click', fetchByAxios); |
40 | 42 | getByXHR.addEventListener('click', fetchByXHR); |
41 | 43 |
|
|
44 | 46 | axios |
45 | 47 | .get('https://dog.ceo/api/breeds/image/random') |
46 | 48 | .then((res) => { |
47 | | - mainDiv.innerHTML = ''; |
| 49 | + let ul = document.createElement("ul"); |
| 50 | + let li = document.createElement("li") |
48 | 51 | let img = document.createElement('img'); |
49 | 52 | img.src = res.data.message; |
50 | | - img.style.width = '400px'; |
51 | | - img.style.height = '400px'; |
52 | | - let h1 = document.createElement('h1'); |
53 | | - h1.innerText = 'Show by Axios'; |
54 | | - mainDiv.appendChild(h1); |
55 | | - mainDiv.appendChild(img); |
| 53 | + let h2 = document.createElement('h2'); |
| 54 | + h2.innerText = 'Show by Axios'; |
| 55 | + li.appendChild(img); |
| 56 | + ul.appendChild(li); |
| 57 | + mainDiv.appendChild(h2); |
| 58 | + mainDiv.appendChild(ul); |
56 | 59 | }) |
57 | 60 | .catch((err) => console.error(err)); |
58 | 61 | } |
|
62 | 65 | let getByXhr = new XMLHttpRequest(); |
63 | 66 | getByXhr.responseType = 'json'; |
64 | 67 | getByXhr.onreadystatechange = function() { |
65 | | - if (this.readyState === 4 && this.status === 200) { |
66 | | - mainDiv.innerHTML = ''; |
| 68 | + if (this.readyState === XMLHttpRequest.DONE && this.status ===200) { |
| 69 | + let ul = document.createElement("ul"); |
| 70 | + let li = document.createElement("li") |
67 | 71 | let img = document.createElement('img'); |
68 | 72 | img.src = getByXhr.response.message; |
69 | | - img.style.width = '400px'; |
70 | | - img.style.height = '400px'; |
71 | | - let h1 = document.createElement('h1'); |
72 | | - h1.innerText = 'Show by XHR'; |
73 | | - mainDiv.appendChild(h1); |
74 | | - mainDiv.appendChild(img); |
| 73 | + let h2 = document.createElement('h2'); |
| 74 | + h2.innerText = 'Show by XHR'; |
| 75 | + li.appendChild(img); |
| 76 | + ul.appendChild(li); |
| 77 | + mainDiv.appendChild(h2); |
| 78 | + mainDiv.appendChild(ul); |
| 79 | + } |
| 80 | + else if (this.status >= 400 && this.status < 500){ |
| 81 | + console.error("There is an error"); |
75 | 82 | } |
76 | 83 | }; |
77 | 84 | getByXhr.open('GET', 'https://dog.ceo/api/breeds/image/random', true); |
|
0 commit comments