본문 바로가기

HTML, CSS

[HTML,CSS] 2주차 Quiz

2022.03.01 - [HTML, CSS] - [HTML,CSS] 2주차 Ajax

 

[HTML,CSS] 2주차 Ajax

Ajax 시작에 앞서 서버→클라이언트 : "JSON" 이해하기 JSON은 Key : Value로 이루어져 있다. 클라이언트→서버 : GET 요청 이해하기 통상적으로! 데이터 조회(Read)를 요청할 때 GET 방식으로 데이터를 전

hoon-language.tistory.com

↑Ajax 기본골격 가져오기

 

 

▶서울시 OpenAPI를 이용하기

* 따릉이 OpenAPI : http://spartacodingclub.shop/sparta_api/seoulbike 

 

<!doctype html>
<html lang="ko">

<head>
    <meta charset="UTF-8">
    <title>JQuery 연습하고 가기!</title>
    <!-- JQuery를 import 합니다 -->
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>

    <style type="text/css">
        div.question-box {
            margin: 10px 0 20px 0;
        }

        table {
            border: 1px solid;
            border-collapse: collapse;
        }

        td,
        th {
            padding: 10px;
            border: 1px solid;
        }
    </style>

    <script>
        function q1() {
            // 여기에 코드를 입력하세요
        }
    </script>

</head>

<body>
    <h1>jQuery + Ajax의 조합을 연습하자!</h1>

    <hr />

    <div class="question-box">
        <h2>2. 서울시 OpenAPI(실시간 따릉기 현황)를 이용하기</h2>
        <p>모든 위치의 따릉이 현황을 보여주세요</p>
        <p>업데이트 버튼을 누를 때마다 지웠다 새로 씌여져야 합니다.</p>
        <button onclick="q1()">업데이트</button>
        <table>
            <thead>
                <tr>
                    <td>거치대 위치</td>
                    <td>거치대 수</td>
                    <td>현재 거치된 따릉이 수</td>
                </tr>
            </thead>
            <tbody id="names-q1">
                <tr>
                    <td>102. 망원역 1번출구 앞</td>
                    <td>22</td>
                    <td>0</td>
                </tr>
                <tr>
                    <td>103. 망원역 2번출구 앞</td>
                    <td>16</td>
                    <td>0</td>
                </tr>
                <tr>
                    <td>104. 합정역 1번출구 앞</td>
                    <td>16</td>
                    <td>0</td>
                </tr>
            </tbody>
        </table>
    </div>
</body>

</html>
$.ajax({
  type: "GET", 
  url: "http://spartacodingclub.shop/sparta_api/seoulbike ", 
  data: {}, 
  success: function(response){
   let rows = response['getStationList']['row']
                    for (i = 0; i < rows.length; i++) {
                        let ddr_name = rows[i]['stationName']
                        let ddr_count = rows[i]['rackTotCnt']
                        let ddr_nowcount = rows[i]['parkingBikeTotCnt']

                        let temp_html = ``
                        if (ddr_nowcount > 4) { // 대수가 5대 미만인 곳을 빨갛게 보여주기
                            temp_html = `<tr>
                              <td>${ddr_name}</td>
                              <td>${ddr_count}</td>
                              <td>${ddr_nowcount}</td>
                              </tr>`
                        } else {
                            temp_html = `<tr class = "bad"> // .bad{color:red;}
                             <td>${ddr_name}</td>
                              <td>${ddr_count}</td>
                              <td>${ddr_nowcount}</td>
                              </tr>`
                        }
                        $('#names-q1').append(temp_html)
  }
})

 

 

▶고양이 OpenAPI

<!doctype html>
<html lang="ko">
  <head>
    <meta charset="UTF-8">
    <title>JQuery 연습하고 가기!</title>
    <!-- JQuery를 import 합니다 -->
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
    
    <style type="text/css">
      div.question-box {
        margin: 10px 0 20px 0;
      }
      div.question-box > div {
        margin-top: 30px;
      }
      
    </style>

    <script>
      function q1() {
        // 여기에 코드를 입력하세요
      }
    </script>

  </head>
  <body>
    <h1>JQuery+Ajax의 조합을 연습하자!</h1>

    <hr/>

    <div class="question-box">
      <h2>3. 랜덤 고양이 사진 API를 이용하기</h2>
      <p>예쁜 고양이 사진을 보여주세요</p>
      <p>업데이트 버튼을 누를 때마다 지웠다 새로 씌여져야 합니다.</p>
      <button onclick="q1()">고양이를 보자</button>
      <div>
        <img id="img-cat" src="https://www.google.com/images/branding/googlelogo/1x/googlelogo_color_272x92dp.png"/>
      </div>
    </div>
  </body>
</html>
$.ajax({
                type: "GET",
                url: "https://api.thecatapi.com/v1/images/search",
                data: {},
                success: function (response) {
                    let imgurl = response[0]['url']
                    $("#img-cat").attr("src", imgurl); // 이미지태그 src 바꾸기
                    }

                })

'HTML, CSS' 카테고리의 다른 글

[HTML,CSS] 2주차 Homework  (0) 2022.03.01
[HTML,CSS] 2주차 Ajax  (0) 2022.03.01
[HTML,CSS] JQuery 연습하기  (0) 2022.03.01
[HTML,CSS] 2주차 JQuery  (0) 2022.03.01
[HTML,CSS] 1주차 완성본  (0) 2022.03.01