File tree Expand file tree Collapse file tree 4 files changed +135
-0
lines changed
Week1/Homework/js-exercises Expand file tree Collapse file tree 4 files changed +135
-0
lines changed Original file line number Diff line number Diff line change
1
+ "use strict"
2
+ const xmlButton = document . querySelector ( "#xml" ) ;
3
+ const axButton = document . querySelector ( "#axios" ) ;
4
+ const ul = document . querySelector ( "ul" ) ;
5
+
6
+
7
+ const xml = new XMLHttpRequest ( ) ;
8
+ let xmData ;
9
+ let axData ;
10
+ let newLi ;
11
+ let newImg ;
12
+
13
+
14
+ function requestXML ( ) {
15
+ xml . onreadystatechange = ( ) => {
16
+ if ( xml . readyState === 4 && xml . status === 200 ) {
17
+ xmData = JSON . parse ( xml . response ) ;
18
+ console . log ( xml . response ) ;
19
+ newLi = document . createElement ( "li" ) ;
20
+ newImg = document . createElement ( "img" ) ;
21
+ newImg . src = xmData . message ;
22
+ newLi . appendChild ( newImg ) ;
23
+ ul . appendChild ( newLi ) ;
24
+
25
+ } else {
26
+ err => console . log ( err )
27
+ }
28
+ }
29
+
30
+ xml . open ( "GET" , "https://dog.ceo/api/breeds/image/random" , true ) ;
31
+ xml . send ( ) ;
32
+ }
33
+ xmlButton . addEventListener ( "click" , requestXML ) ;
34
+
35
+ function requestAxios ( ) {
36
+ axios
37
+ . get ( "https://dog.ceo/api/breeds/image/random" )
38
+ . then ( resp => {
39
+ console . log ( resp ) ;
40
+ axData = resp . data . message ;
41
+ newLi = document . createElement ( "li" ) ;
42
+ newImg = document . createElement ( "img" ) ;
43
+ newImg . src = axData ;
44
+ newLi . appendChild ( newImg ) ;
45
+ ul . appendChild ( newLi ) ;
46
+
47
+ } )
48
+ . catch ( err => console . log ( err ) )
49
+ }
50
+ axButton . addEventListener ( "click" , requestAxios )
Original file line number Diff line number Diff line change
1
+ <!DOCTYPE html>
2
+ < html lang ="en ">
3
+
4
+ < head >
5
+ < meta charset ="UTF-8 ">
6
+ < meta name ="viewport " content ="width=device-width, initial-scale=1.0 ">
7
+ < title > Dog Gallery</ title >
8
+ </ head >
9
+
10
+ < body >
11
+ < button id ="xml "> XML</ button >
12
+ < button id ="axios "> AXIOS</ button >
13
+ < div >
14
+ < ul >
15
+
16
+ </ ul >
17
+
18
+ </ div >
19
+ < script src ="https://unpkg.com/axios/dist/axios.min.js "> </ script >
20
+ < script src ="dogPhoto.js "> </ script >
21
+ </ body >
22
+
23
+ </ html >
Original file line number Diff line number Diff line change
1
+ <!DOCTYPE html>
2
+ < html lang ="en ">
3
+
4
+ < head >
5
+ < meta charset ="UTF-8 ">
6
+ < meta name ="viewport " content ="width=device-width, initial-scale=1.0 ">
7
+ < title > progHumor</ title >
8
+ </ head >
9
+
10
+ < body >
11
+ < button id ="xml "> XML</ button >
12
+ < button id ="axios "> AXIOS</ button >
13
+ < br >
14
+ < img src ="" id ="cat-image " width ="500px " style ="border: 2px solid grey; border-radius: 10px; margin: 20px; ">
15
+
16
+
17
+ < script src ="https://unpkg.com/axios/dist/axios.min.js "> </ script >
18
+ < script src ="progHumor.js "> </ script >
19
+ </ body >
20
+
21
+
22
+ </ html >
Original file line number Diff line number Diff line change
1
+ "use strict"
2
+ //Due to a CORS error I couldn't use the API of the exercise so I used another API with the same logic.
3
+ //Each time you press a button a request is being sent, the data is logged in the console, and an image of a cute cat appears.
4
+ const xmlButton = document . querySelector ( "#xml" ) ;
5
+ const axiosButton = document . querySelector ( "#axios" ) ;
6
+ const textbox = document . querySelector ( "#textbox" ) ;
7
+ const image = document . querySelector ( "#cat-image" )
8
+
9
+ const xhr = new XMLHttpRequest ( ) ;
10
+ let xmlData ;
11
+ let axiosData ;
12
+
13
+
14
+ function xmlReq ( ) {
15
+ xhr . onreadystatechange = ( ) => {
16
+ if ( xhr . readyState === 4 && xhr . status === 200 ) {
17
+ xmlData = JSON . parse ( xhr . response ) ;
18
+ console . log ( xmlData [ 0 ] ) ;
19
+ image . src = xmlData [ 0 ] . url ;
20
+ } else {
21
+ err => console . log ( err )
22
+ }
23
+ }
24
+ xhr . open ( "GET" , "https://api.thecatapi.com/v1/images/search" , true ) ;
25
+ xhr . send ( ) ;
26
+ }
27
+ xmlButton . addEventListener ( "click" , xmlReq ) ;
28
+
29
+ function axiosReq ( ) {
30
+ axios
31
+ . get ( "https://api.thecatapi.com/v1/images/search" )
32
+ . then ( response => {
33
+ console . log ( response . data [ 0 ] ) ;
34
+ axiosData = response . data [ 0 ] ;
35
+ image . src = axiosData . url ;
36
+ } )
37
+ . catch ( error => console . log ( error ) )
38
+ }
39
+
40
+ axiosButton . addEventListener ( "click" , axiosReq ) ;
You can’t perform that action at this time.
0 commit comments