Repository navigation
Expand file tree
/
Copy pathdogGallery.js
More file actions
59 lines (46 loc) · 1.56 KB
/
Copy pathdogGallery.js
File metadata and controls
59 lines (46 loc) · 1.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
const XMLBtn = document.getElementById('btn1');
const AxiosBtn = document.getElementById('btn2');
function xhrMethod() {
const xhr = new XMLHttpRequest();
const li = document.createElement('li');
const image = document.createElement('img');
const ul = document.getElementById('list');
const url = 'https://dog.ceo/api/breeds/image/random'
xhr.open('GET', url);
xhr.send();
xhr.onreadystatechange = processRequest;
ul.appendChild(li.appendChild(image))
function processRequest(e) {
if(xhr.readyState == 4 && xhr.status == 200) {
let response = JSON.parse(xhr.responseText);
image.src = response.message;
image.style.width = '300px'
image.style.height = '300px'
image.style.margin = '5px'
};
};
xhr.onerror = function() {
if (xhr.status != 200) {
alert (`Error ${xhr.status}: ${xhr.statusText}`);
};
};
};
XMLBtn.addEventListener('click', xhrMethod);
function axiosMethod() {
const li = document.createElement('li');
const image = document.createElement('img');
const ul = document.getElementById('list');
ul.appendChild(li.appendChild(image))
axios
.get('https://dog.ceo/api/breeds/image/random')
.then(function(response) {
image.src = response.data.message;
image.style.width = '300px'
image.style.height = '300px'
image.style.margin = '5px'
})
.catch(function(error) {
console.log(error)
});
}
AxiosBtn.addEventListener('click', axiosMethod);