Ajax에서 명절 일람을 얻다

개요

  • Ajax에서 공휴일 일람을 얻고 표시된 비망록
    →사용Holidays JP API | github.io
  • 참고란의 페이지만 참고하여 쓴다
  • 코드


    holiday_list.html
    <!doctype html>
    
    <html lang="ja">
    <head>
      <meta charset="utf-8">
    
      <title>祝日一覧</title>
      <meta name="description" content="The Holiday List">
      <meta name="author" content="SitePoint">
      <!-- cssファイルがある場合は読み込み -->
      <!--<link rel="stylesheet" href="css/styles.css"> -->
    </head>
    
    <body>
      <!-- jQueryのCDN読み込み -->
      <script src="https://code.jquery.com/jquery-3.6.0.js" integrity="sha256-H+K7U5CnXl1h5ywQfKtSj8PCmoN9aaq30gDh27Xc0jk=" crossorigin="anonymous"></script>
      <script>
        $(function(){
          $.ajax({
              url: 'https://holidays-jp.github.io/api/v1/date.json',
              type: 'GET',
              dataType: 'json',
              timeout: 5000
          }).done(function(data){
            /* 通信成功時 */
            $('body').append('<table></table>');
            $('table').attr('border', 1);
            $('table').attr('style', "border-collapse: collapse");
            $('table').append('<tr><th>日付</th><th>名称</th></tr>');
            Object.keys(data).forEach((key) => {
              // $('table').append(`<tr><td>${key}</td></tr>`);
              $('table').append(`<tr><td>${key}</td><td>${data[key]}</td></tr>`);
            });
            $('th').css('background-color', '#CCCCFF');
            $('td').attr('align', 'center');
          }).fail(function(data){
            /* 通信失敗時 */
            $('body').append('<p></p>');
            $('p').append('通信に失敗しました');
          }).always(function(data){
            /* 通信成功・失敗問わず */
            $('body').append('<p></p>');
            $('p').append('以上です');
          });
        });
      </script>
      <!-- javascriptがある場合は読み込み -->
      <!-- <script src="js/scripts.js"></script> -->
    </body>
    </html>
    

    참고 자료

  • Holidays JP API | github.io
  • [초학] 첫 API(fetch와promise 따위는 잘 몰라요)|Qita
  • jQuery의 DOM 조작 요소를 이용한 추가 방법 요약|Qita
  • JS의 Object를 forEach로 처리하는 방법
  • setTimeout의 0밀리초 후는
  • jQuery로 Ajax 통신하는 방법[샘플 코드로 설명] | 버섯 로그
  • 좋은 웹페이지 즐겨찾기