728x90
반응형
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 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 | <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Dog API</title> <style> img { height: 500px; } </style> </head> <body> <h1>Dog API</h1> <img id="dog-img" src="" alt="dog"> <br> <button id="dbtn">Get dog</button> <h1>Cat API</h1> <img id="cat-img" src="" alt="cat"> <br> <button id="cbtn">Get cat</button> <!-- axios CDN을 삽입한다. --> <script src="https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js"></script> <script> /////////////////////////////////////////////////////////////////dog const API_URI = 'https://dog.ceo/api/breeds/image/random' // 교수님 코드 // function getDog() { // axios.get(API_URI) // == requests.get(API_URL) // .then((res) => { // const dogImgUrl = res.data.message // const img = document.querySelector('#dog-img') // img.src = dogImgUrl // }) // .catch((err) => { // console.error(err) // }) // } function getDog () { // axios를 사용하여 API_URI로 GET 요청을 보낸다. axios.get(API_URI) // .then 메서드를 통해 요청이 성공적인 경우의 콜백함수를 정의한다. .then((response) => { //console.log(response) // 데이터가 data의 message에 링크있어서 바로! return response.data.message }) // 응답객체의 데이터에서 이미지에 대한 리소스를 img 요소의 src 속성으로 할당한다. .then((response) => { const Img = document.querySelector('#dog-img') Img.src = response }) } const button = document.querySelector('#dbtn') button.addEventListener('click', getDog) ////////////////////////////////////////////////////////////////////cat const API_URIc = 'https://api.thecatapi.com/v1/images/search' function getCat () { // axios를 사용하여 API_URI로 GET 요청을 보낸다. axios.get(API_URIc) // .then 메서드를 통해 요청이 성공적인 경우의 콜백함수를 정의한다. .then((response) => { //console.log(response) // data가 array 형식의 0에 url 을 포함하고있기문에 return response.data[0].url }) // 응답객체의 데이터에서 이미지에 대한 리소스를 img 요소의 src 속성으로 할당한다. .then((response) => { const catImg = document.querySelector('#cat-img') catImg.src = response }) } const button2 = document.querySelector('#cbtn') button2.addEventListener('click', getCat) </script> </body> </html> | cs |
728x90
반응형
'SSAFY > Django' 카테고리의 다른 글
[JS] Vue 기본 (0) | 2021.05.08 |
---|---|
[Django] 게시판, 좋아요, 댓글, 계정 기능 종합 (0) | 2021.05.02 |
Dom 실무? (0) | 2021.04.29 |
[Django] Dom (0) | 2021.04.28 |
[Django] swagger (0) | 2021.04.27 |