2์ฃผ์ฐจ WIL๐Ÿฑโ€๐Ÿ‘คJavascript,JQuery,Ajax

Winteringยท2022๋…„ 4์›” 5์ผ
0

๋‚ด์ผ๋ฐฐ์›€์บ ํ”„

๋ชฉ๋ก ๋ณด๊ธฐ
2/17

Javascript ๋ณต์Šต๐ŸŽ€

  • ํ•จ์ˆ˜์˜ ๋ณ€์ˆ˜ ์ง€์ •ํ•˜๊ธฐ

    • ํด๋ฆญ ํšŸ์ˆ˜์— ๋”ฐ๋ผ ์ง์ˆ˜,ํ™€์ˆ˜ alert

      function hey(){
              let count = 1;
                  if (count % 2 == 0){
                      alert('์ง์ˆ˜์ž…๋‹ˆ๋‹ค.');
                  }else{
                      alert('ํ™€์ˆ˜์ž…๋‹ˆ๋‹ค.');
                  }
                  count += 1;
              }

      ๊ฒฐ๊ณผ : ๋ช‡ ๋ฒˆ์„ ๋ˆ„๋ฅด๋“  'ํ™€์ˆ˜์ž…๋‹ˆ๋‹ค'๋งŒ ์ถœ๋ ฅ๋จ / count ๋ณ€์ˆ˜๊ฐ€ ํ•จ์ˆ˜ ์•ˆ์— ์žˆ๊ธฐ ๋•Œ๋ฌธ์—, ๋งˆ์ง€๋ง‰ count += 1์ด ๋๋”๋ผ๋„, ๋‹ค์‹œ ์ฒซ์งธ ์ค„๋กœ ๋Œ์•„๊ฐˆ ๋•Œ 1๋กœ ์ดˆ๊ธฐํ™”

      let count = 1;    							//count ๋ณ€์ˆ˜๋ฅผ ํ•จ์ˆ˜์˜ ๋ฐ–์œผ๋กœ ๋นผ์คŒ (์ „์—ญ๋ณ€์ˆ˜)
      function hey(){
                  if (count % 2 == 0){
                      alert('์ง์ˆ˜์ž…๋‹ˆ๋‹ค.');
                  }else{
                      alert('ํ™€์ˆ˜์ž…๋‹ˆ๋‹ค.');
                  }
                  count += 1;
              }

      ๊ฒฐ๊ณผ : ํด๋ฆญ์— ๋”ฐ๋ผ์„œ 'ํ™€์ˆ˜,์ง์ˆ˜'๊ฐ€ ์˜ณ๊ฒŒ ์ถœ๋ ฅ๋จ / count๋ณ€์ˆ˜๊ฐ€ ํ•จ์ˆ˜ ๋ฐ–์œผ๋กœ ๋น ์ ธ๋‚˜๊ฐ€์„œ count += 1; ์— ๋”ฐ๋ผ ๊ฐ’์ด ๋ณ€ํ•  ์ˆ˜ ์žˆ๊ฒŒ ๋จ

      let temp_html = '๋‚˜๋Š” ์ถ”๊ฐ€๋  ๋ฒ„ํŠผ์ด๋‹ค!';
      $('#cards-box').append(temp_html);Javascript ๋ณต์Šต๐ŸŽ€

  • ํ•จ์ˆ˜์˜ ๋ณ€์ˆ˜ ์ง€์ •ํ•˜๊ธฐ

    • ํด๋ฆญ ํšŸ์ˆ˜์— ๋”ฐ๋ผ ์ง์ˆ˜,ํ™€์ˆ˜ alert
    function hey(){
            let count = 1;
                if (count % 2 == 0){
                    alert('์ง์ˆ˜์ž…๋‹ˆ๋‹ค.');
                }else{
                    alert('ํ™€์ˆ˜์ž…๋‹ˆ๋‹ค.');
                }
                count += 1;
            }

    ๊ฒฐ๊ณผ : ๋ช‡ ๋ฒˆ์„ ๋ˆ„๋ฅด๋“  'ํ™€์ˆ˜์ž…๋‹ˆ๋‹ค'๋งŒ ์ถœ๋ ฅ๋จ / count ๋ณ€์ˆ˜๊ฐ€ ํ•จ์ˆ˜ ์•ˆ์— ์žˆ๊ธฐ ๋•Œ๋ฌธ์—, ๋งˆ์ง€๋ง‰ count += 1์ด ๋๋”๋ผ๋„, ๋‹ค์‹œ ์ฒซ์งธ ์ค„๋กœ ๋Œ์•„๊ฐˆ ๋•Œ 1๋กœ ์ดˆ๊ธฐํ™”

    let count = 1;    							//count ๋ณ€์ˆ˜๋ฅผ ํ•จ์ˆ˜์˜ ๋ฐ–์œผ๋กœ ๋นผ์คŒ (์ „์—ญ๋ณ€์ˆ˜)
    function hey(){
                if (count % 2 == 0){
                    alert('์ง์ˆ˜์ž…๋‹ˆ๋‹ค.');
                }else{
                    alert('ํ™€์ˆ˜์ž…๋‹ˆ๋‹ค.');
                }
                count += 1;
            }

    ๊ฒฐ๊ณผ : ํด๋ฆญ์— ๋”ฐ๋ผ์„œ 'ํ™€์ˆ˜,์ง์ˆ˜'๊ฐ€ ์˜ณ๊ฒŒ ์ถœ๋ ฅ๋จ / count๋ณ€์ˆ˜๊ฐ€ ํ•จ์ˆ˜ ๋ฐ–์œผ๋กœ ๋น ์ ธ๋‚˜๊ฐ€์„œ count += 1; ์— ๋”ฐ๋ผ ๊ฐ’์ด ๋ณ€ํ•  ์ˆ˜ ์žˆ๊ฒŒ ๋จ

JQuery ๐ŸŽ€

: HTML์˜ ์š”์†Œ๋“ค์„ ์กฐ์ž‘ํ•˜๋Š” ํŽธ๋ฆฌํ•œ Javascript๋ฅผ ๋ฏธ๋ฆฌ ์ž‘์„ฑํ•ด๋‘”๊ฒƒ, ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ

Javascript๋กœ๋„ ๋ชจ๋“  ๊ธฐ๋Šฅ์„ ๊ตฌํ˜„์€ ๊ฐ€๋Šฅ
๋‹ค๋งŒ ์ฝ”๋“œ๊ฐ€ ๋ณต์žกํ•˜๊ณ , ๋ธŒ๋ผ์šฐ์ € ๊ฐ„์˜ ํ˜ธํ™˜์„ฑ ๋ฌธ์ œ๊ฐ€ ์žˆ์Œ

//Javascript
document.getElementById("element").style.display = "none";
//JQuery
$('#element').hide();
  • JQuery๋Š” ๋ฏธ๋ฆฌ ์ž‘์„ฑ๋œ Javascript์ฝ”๋“œ. ์“ฐ๊ธฐ์ „์— ๋ฐ˜๋“œ์‹œ ์ž„ํฌํŠธํ•ด์•ผํ•จ

    https://www.w3schools.com/jquery/jquery_get_started.asp

    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
    • ๋ถ€ํŠธ์ŠคํŠธ๋žฉ์˜ ๊ฒฝ์šฐ ์ด๋ฏธ JQuery๊ฐ€ ์ž„ํฌํŠธ ๋˜์–ด์žˆ๊ธฐ ๋•Œ๋ฌธ์— ๋”ฐ๋กœ ์ถ”๊ฐ€ํ•  ํ•„์š”๋Š” X
    • ๋ถ€ํŠธ์ŠคํŠธ๋žฉ์„ ์‚ฌ์šฉํ•˜์ง€ ์•Š์€ ๊ฒฝ์šฐ๋Š” ์ถ”๊ฐ€ํ•ด์ค˜์•ผํ•จ!โœ”
    • jquery๋Š” id๋กœ ํ˜ธ์ถœํ•˜๋Š” ๋“ฏ, id๊ฐ’์„ ์ถ”๊ฐ€ํ•ด์ค˜์•ผํ•˜๋Š” ๋“ฏ!

1. input๋ฐ•์Šค์˜ ๊ฐ’์„ ๊ฐ€์ ธ์™€๋ณด๊ธฐ

$('#article-url').val()			//#์ž๋ฆฌ์—๋Š” input๋ฐ•์Šค์˜ id๊ฐ’

2. div ์‚ฌ๋ผ์ง€๊ณ  ๋‚˜ํƒ€๋‚˜๊ฒŒ ํ•˜๊ธฐ

$('#element').hide();			//#์ž๋ฆฌ์—๋Š” id๊ฐ’ 
$('#element').show();

3. css๊ฐ’ ๊ฐ€์ ธ์™€๋ณด๊ธฐ

$('#post-box').hide();
$('#post-box').css('display');

$('#post-box').show();
$('#post-box').css('display');

4. ํƒœ๊ทธ ๋‚ด ํ…์ŠคํŠธ ์ž…๋ ฅํ•˜๊ธฐ

$('#post-url').val('์—ฌ๊ธฐ์— ํ…์ŠคํŠธ๋ฅผ ์ž…๋ ฅํ•˜๋ฉด!');
// ๊ฐ€๋ฆฌํ‚ค๊ณ  ์‹ถ์€ ๋ฒ„ํŠผ์— id ๊ฐ’์„ ์ค€๋‹ค์Œ
<button id="btn-posting-box" type="button" class="btn btn-primary">ํฌ์ŠคํŒ… ๋ฐ•์Šค ์—ด๊ธฐ</button>
$('#btn-posting-box').text('ํฌ์ŠคํŒ… ๋ฐ•์Šค ๋‹ซ๊ธฐ');
  • .val์€ input box์—๋งŒ ์‚ฌ์šฉ / .text๋Š” ํ…์ŠคํŠธ๋ฅผ ๋ฐ”๊ฟ€ ๋•Œ ์ฃผ๋กœ ๋งŽ์ด ์‚ฌ์šฉ

5. ํƒœ๊ทธ ๋‚ด html ์ž…๋ ฅํ•˜๊ธฐ

<div id="cards-box" class="card-columns">			//id = "cards-box"๋ฅผ ์คŒ
    <div class="card">
      <img class="card-img-top" src="https://www.fodors.com/wp-content/uploads/2018/10/4_UltimateRome_PiazzaNavona-975x650.jpg" alt="Card image cap">
      <div class="card-body">
        <a href="https://naver.com/" class="card-title">์—ฌ๊ธฐ ๊ธฐ์‚ฌ ์ œ๋ชฉ์ด ๋“ค์–ด๊ฐ€์ฃ </a>
        <p class="card-text">์—ฌ๊ธฐ์— ๊ธฐ์‚ฌ ๋‚ด์šฉ์ด ๋“ค์–ด๊ฐ€๊ฒ ์ฃ </p>
        <p class="card-text comment">์—ฌ๊ธฐ์—” ์ฝ”๋ฉ˜ํŠธ๊ฐ€ ๋“ค์–ด๊ฐ‘๋‹ˆ๋‹ค</p>
      </div>
    </div>
  </div>
let temp_html = '<button>๋‚˜๋Š” ์ถ”๊ฐ€๋  ๋ฒ„ํŠผ์ด๋‹ค!</button>';	//temp_html ๋ณ€์ˆ˜๋ฅผ ์ง€์ •
$('#cards-box').append(temp_html);
// ์ฃผ์˜: ํ™‘๋”ฐ์˜ดํ‘œ(')๊ฐ€ ์•„๋‹Œ backtick(`)์‚ฌ์šฉ
// backtick์„ ์‚ฌ์šฉํ•˜๋ฉด ๋ฌธ์ž ์ค‘๊ฐ„์— Javascript ๋ณ€์ˆ˜๋ฅผ ์‚ฝ์ž…๊ฐ€๋Šฅ
let img_url = 'https://www.eurail.com/content/dam/images/eurail/Italy%20OCP%20Promo%20Block.adaptive.767.1535627244182.jpg';
let link_url = 'https://naver.com/';
let title = '์—ฌ๊ธฐ ๊ธฐ์‚ฌ ์ œ๋ชฉ์ด ๋“ค์–ด๊ฐ€์ฃ ';
let desc = '๊ธฐ์‚ฌ์˜ ์š”์•ฝ ๋‚ด์šฉ์ด ๋“ค์–ด๊ฐ‘๋‹ˆ๋‹ค. ๋™ํ•ด๋ฌผ๊ณผ ๋ฐฑ๋‘์‚ฐ์ด ๋งˆ๋ฅด๊ณ  ๋‹ณ๋„๋ก ํ•˜๋Š๋‹˜์ด ๋ณด์šฐํ•˜์‚ฌ ์šฐ๋ฆฌ๋‚˜๋ผ๋งŒ์„ธ ๋ฌด๊ถํ™” ์‚ผ์ฒœ๋ฆฌ ํ™”๋ ค๊ฐ•์‚ฐ...';
let comment = '์—ฌ๊ธฐ์— ์ฝ”๋ฉ˜ํŠธ๊ฐ€ ๋“ค์–ด๊ฐ‘๋‹ˆ๋‹ค.';

let temp_html = `<div class="card">
                    <img class="card-img-top"
                        src="${img_url}"
                        alt="Card image cap">
                    <div class="card-body">
                        <a href="${link_url}" class="card-title">${title}</a>
                        <p class="card-text">${desc}</p>
                        <p class="card-text comment">${comment}</p>
                    </div>
                </div>`;
$('#cards-box').append(temp_html);

6. JQuery ํ€ด์ฆˆ

<!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;
        }
    </style>

    <script>
        function q1() {
            let input_text = $('#input-q1').val();
            if(input_text == ''){
                   alert('์ž…๋ ฅํ•˜์„ธ์š”!!');
            }else{
                   alert(input_text);
            }
        }

        function q2() {
            let input_text2 = $('#input-q2').val();
            if(input_text2.includes('@')){
                alert(input_text2.split('@')[1].split('.')[0]);
            }else{
                alert('์ด๋ฉ”์ผ์ด ์•„๋‹™๋‹ˆ๋‹ค.');
            }
        }

        function q3() {
            let input_text3 = $('#input-q3').val();
            if(input_text3 == '') {
                alert('์ด๋ฆ„์„ ์ž…๋ ฅํ•˜์„ธ์š”.');
            }else{
                let temp_html = `<li>${input_text3}</li>`; //๊ผฎ!!!!๋ฐฑํ‹ฑ์œผ๋กœ ๊ฐ์‹ธ๊ธฐ!!!!!!!
                $('#names-q3').append(temp_html);
            }
        }

        function q3_remove() {
              $('#names-q3').empty();
        }

    </script>

</head>

<body>
    <h1>jQuery + Javascript์˜ ์กฐํ•ฉ์„ ์—ฐ์Šตํ•˜์ž!</h1>

    <div class="question-box">
        <h2>1. ๋นˆ์นธ ์ฒดํฌ ํ•จ์ˆ˜ ๋งŒ๋“ค๊ธฐ</h2>
        <h5>1-1. ๋ฒ„ํŠผ์„ ๋ˆŒ๋ €์„ ๋•Œ ์ž…๋ ฅํ•œ ๊ธ€์ž๋กœ ์–ผ๋Ÿฟ ๋„์šฐ๊ธฐ</h5>
        <h5>[์™„์„ฑ๋ณธ]1-2. ๋ฒ„ํŠผ์„ ๋ˆŒ๋ €์„ ๋•Œ ์นธ์— ์•„๋ฌด๊ฒƒ๋„ ์—†์œผ๋ฉด "์ž…๋ ฅํ•˜์„ธ์š”!" ์–ผ๋Ÿฟ ๋„์šฐ๊ธฐ</h5>
        <input id="input-q1" type="text" /> <button onclick="q1()">ํด๋ฆญ</button>
    </div>
    <hr />
    <div class="question-box">
        <h2>2. ์ด๋ฉ”์ผ ํŒ๋ณ„ ํ•จ์ˆ˜ ๋งŒ๋“ค๊ธฐ</h2>
        <h5>2-1. ๋ฒ„ํŠผ์„ ๋ˆŒ๋ €์„ ๋•Œ ์ž…๋ ฅ๋ฐ›์€ ์ด๋ฉ”์ผ๋กœ ์–ผ๋Ÿฟ ๋„์šฐ๊ธฐ</h5>
        <h5>2-2. ์ด๋ฉ”์ผ์ด ์•„๋‹ˆ๋ฉด(@๊ฐ€ ์—†์œผ๋ฉด) '์ด๋ฉ”์ผ์ด ์•„๋‹™๋‹ˆ๋‹ค'๋ผ๋Š” ์–ผ๋Ÿฟ ๋„์šฐ๊ธฐ</h5>
        <h5>[์™„์„ฑ๋ณธ]2-3. ์ด๋ฉ”์ผ ๋„๋ฉ”์ธ๋งŒ ์–ผ๋Ÿฟ ๋„์šฐ๊ธฐ</h5>
        <input id="input-q2" type="text" /> <button onclick="q2()">ํด๋ฆญ</button>
    </div>
    <hr />
    <div class="question-box">
        <h2>3. HTML ๋ถ™์ด๊ธฐ/์ง€์šฐ๊ธฐ ์—ฐ์Šต</h2>
        <h5>3-1. ์ด๋ฆ„์„ ์ž…๋ ฅํ•˜๋ฉด ์•„๋ž˜ ๋‚˜์˜ค๊ฒŒ ํ•˜๊ธฐ</h5>
        <h5>[์™„์„ฑ๋ณธ]3-2. ๋‹ค์ง€์šฐ๊ธฐ ๋ฒ„ํŠผ์„ ๋งŒ๋“ค๊ธฐ</h5>
        <input id="input-q3" type="text" placeholder="์—ฌ๊ธฐ์— ์ด๋ฆ„์„ ์ž…๋ ฅ" />
        <button onclick="q3()">์ด๋ฆ„ ๋ถ™์ด๊ธฐ</button>
        <button onclick="q3_remove()">๋‹ค์ง€์šฐ๊ธฐ</button>
        <ul id="names-q3">
            <li>์„ธ์ข…๋Œ€์™•</li>
            <li>์ž„๊บฝ์ •</li>
        </ul>
    </div>
</body>

</html>
 let temp_html = `<li>${input_text3}</li>`; 
  • ${input_text3}
  • ๋ถ€๋ถ„์„ ์ฒ˜์Œ์—๋Š” ์•„๋ฌด๊ฒƒ๋„ ์•ˆ๊ฐ์‹ธ์„œ ์˜ค๋ฅ˜ ๋ฐœ์ƒ
  • '' ํ™‘๋”ฐ์˜ดํ‘œ๋กœ ๊ฐ์‹ธ์„œ ์˜ค๋ฅ˜๋ฐœ์ƒ. ์ถœ๋ ฅ๋‹จ์— ์ž…๋ ฅํ•œ ์ด๋ฆ„์ด ์•„๋‹Œ input_text3๊ฐ€ ์ถœ๋ ฅ๋จ
  • `` ์œผ๋กœ ๊ฐ์‹ผ ๋‹ค์Œ์—์•ผ ์ œ๋Œ€๋กœ ์‹คํ–‰ .... ๋งค์šฐ ์ค‘์š”.. ๋ฐฑํ‹ฑ... ๐Ÿ’ฆ

์„œ๋ฒ„ > ํด๋ผ์ด์–ธํŠธ ํ†ต์‹  (JSON)๐ŸŽ€

  • ์„œ๋ฒ„ > ํด๋ผ์ด์–ธํŠธ : JSON ์ดํ•ดํ•˜๊ธฐ

    • ํด๋ผ์ด์–ธํŠธ๊ฐ€ ์„œ๋ฒ„์— ๋ฐ์ดํ„ฐ ์š”์ฒญ (์ค„๊ฑฐ์—†๋‹ˆ?)
    • ์„œ๋ฒ„๊ฐ€ ํด๋ผ์ด์–ธํŠธ์˜ ์š”์ฒญ์„ ๋ฐ›์•„ ๋ฐ์ดํ„ฐ๋ฅผ ์คŒ (๊ฐ€์ ธ๊ฐ€!) > ์ด๋•Œ ๋‚ด๋ ค์ฃผ๋Š” ํฌ๋งท : JSON
  • JSON

    • ๋”•์…”๋„ˆ๋ฆฌ + ๋ฆฌ์ŠคํŠธ์˜ ์กฐํ•ฉ๊ณผ ๊ฐ™๊ฒŒ ์ƒ๊น€!

    • ex) ๋ฏธ์„ธ๋จผ์ง€ Open API

      image-20220407041803152

      • for๋ฌธ์„ ์‚ฌ์šฉํ•˜๋Š” ์ „ํ˜•์ ์ธํ˜•ํƒœ!

ํด๋ผ์ด์–ธํŠธ > ์„œ๋ฒ„ ํ†ต์‹  (GET,POST ์š”์ฒญ)๐ŸŽ€

  • GET : ๋ฐ์ดํ„ฐ ์กฐํšŒ(Read)๋ฅผ ์š”์ฒญํ•  ๋•Œ

    ex) ์˜ํ™” ๋ชฉ๋ก ์กฐํšŒ

    https://movie.naver.com/movie/bi/mi/basic.nhn?code=161967

    โœ”?๋ฅผ ๊ธฐ์ ์œผ๋กœ ๋’ค๋Š” ํด๋ผ์ด์–ธํŠธ๊ฐ€ ๊ฐ–๊ณ ์˜ค๋Š” ์ •๋ณด

  • POST : ๋ฐ์ดํ„ฐ ์ƒ์„ฑ(Create), ๋ณ€๊ฒฝ(Update), ์‚ญ์ œ(Delete)๋ฅผ ์š”์ฒญํ•  ๋•Œ

    ex) ํšŒ์›๊ฐ€์ž…, ํšŒ์›ํƒˆํ‡ด, ๋น„๋ฐ€๋ฒˆํ˜ธ ์ˆ˜์ •

Ajax๐ŸŽ€

๐Ÿ”ปAjax๋Š” JQuery๋ฅผ ์ž„ํฌํŠธํ•œ ํŽ˜์ด์ง€์—์„œ๋งŒ ์ž‘๋™!!!๐Ÿ”ป

  • Ajax ๊ธฐ๋ณธ๊ณจ๊ฒฉ
$.ajax({
  type: "GET",									// ํƒ€์ž…์€ get
  url: "์—ฌ๊ธฐ์—URL์„์ž…๋ ฅ",							// resopnse๋ฅผ ๋ฐ›์•„์˜ฌ url
  data: {},
  success: function(response){
    console.log(response)
  }
})

ajax OpenAPI ๋”ฐ๋ฆ‰์ด ํ€ด์ฆˆ๐ŸŽ€ (for๋ฌธ, if๋ฌธ ์‘์šฉ)

<!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;
        }

        .lack{
            color: red;
        }
    </style>

    <script>
        function q1() {
            $.ajax({
                type: "GET",
                url: "http://spartacodingclub.shop/sparta_api/seoulbike",							// resopnse๋ฅผ ๋ฐ›์•„์˜ฌ url
                data: {},
                success: function (response) {
                    let rows = response['getStationList']['row'];
                    for (let i = 0; i < rows.length; i++) {
                        let st_name = rows[i]['stationName'];
                        let rack = rows[i]['rackTotCnt'];
                        let parking_bike = rows[i]['parkingBikeTotCnt'];

                        let text_html = ""

                        if (rack < 5) {
                            text_html = `<tr class="lack"><td>${st_name}</td>
                                            <td>${rack}</td>
                                            <td>${parking_bike}</td></tr>`
                        } else {
                            text_html = `<tr><td>${st_name}</td>
                                            <td>${rack}</td>
                                            <td>${parking_bike}</td></tr>`
                        }

                        $('#names-q1').append(text_html);
                    }
                }
            })
        }
    </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">
        </tbody>
    </table>
</div>
</body>

</html>

๋žœ๋ค ๊ฐ•์•„์ง€ ์‚ฌ์ง„ API๐ŸŽ€

<!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() {
            $.ajax({
                type: "GET",
                url: "https://api.thedogapi.com/v1/images/search",
                data: {},
                success: function (response) {
                    let imgSrc = response[0]['url'];
                    $("#img-dog").attr("src", imgSrc);
                }
            })
        }
    </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-dog" src="https://www.google.com/images/branding/googlelogo/1x/googlelogo_color_272x92dp.png"/>
    </div>
</div>
</body>
</html>

โœ” Poin1tโœ”

  <script>
        function q1() {
            $.ajax({
                type: "GET",
                url: "https://api.thedogapi.com/v1/images/search",
                data: {},
                success: function (response) {
                    let imgSrc = response[0]['url'];	<!-- ๋ฆฌ์ŠคํŠธ์ค‘ 0๋ฒˆ์งธ์˜, url! -->
                    $("#img-dog").attr("src", imgSrc);
                }
            })
        }
    </script>

image-20220408143430529

: API ์ฃผ์†Œ ์ž์ฒด์—์„œ JSON์ด ์ฒ˜์Œ๋ถ€ํ„ฐ ๋ฆฌ์ŠคํŠธ ํ˜•์‹์œผ๋กœ ๋“ค์–ด์™€ ์žˆ์Œ / ์›ํ•˜๋Š” url ์ฃผ์†Œ๋ฅผ ์–ป์€ ํ‘œํ˜„์‹ ๊ธฐ์–ต!

โœ” Poin1tโœ”

<img id="id_img">

์•„์ด๋”” ํƒœ๊ทธ์— ๋”ฐ๋ฅธ ์ด๋ฏธ์ง€ src๋ฅผ ๋ณ€๊ฒฝํ•˜๋Š” ๋ฐฉ๋ฒ•

$("#id_img").attr("src", "์ด๋ฏธ์ง€ ๊ฒฝ๋กœ");

0๊ฐœ์˜ ๋Œ“๊ธ€