[JavaScript] AJAX(Asynchronous JavaScript And XML)


1. AJAX (Asynchronous JavaScript And XML)

(1): ํŠน์ง•

๋น„๋™๊ธฐ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ

๋น„๋™๊ธฐ์  ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ๋™์ž‘์„ ํ•˜๋Š” ๊ธฐ์ˆ 

์„œ๋ฒ„๋กœ๋ถ€ํ„ฐ์˜ ์‘๋‹ต(response) ํ™•์ธ

XMLHttpRequest`ย ๊ฐ์ฒด๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์„ ๋งํ•จ

ํŽ˜์ด์ง€์˜ ์ƒˆ๋กœ๊ณ ์นจ ์—†์ด๋„ URL์—์„œ ๋ฐ์ดํ„ฐ๋ฅผ ๊ฐ€์ ธ์˜ฌ ์ˆ˜ ์žˆ์Œ.

(2): ํŠน์ง•

  • ํŽ˜์ด์ง€ ์ƒˆ๋กœ๊ณ ์นจ ์—†์ด ์„œ๋ฒ„์— ์š”์ฒญ
  • ์„œ๋ฒ„๋กœ๋ถ€ํ„ฐ ๋ฐ์ดํ„ฐ๋ฅผ ๋ฐ›๊ณ  ์ž‘์—…์„ ์ˆ˜ํ–‰

1) XMLHttpRequest

[XMLHttpRequest - Web APIMDN](https://developer.mozilla.org/ko/docs/Web/API/XMLHttpRequest)

ํŠน์ง•

  • ์ดˆ๊ธฐ ๊ธฐํš๋•Œ๋ถ€ํ„ฐ ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ์ œ๊ณตํ•˜์ง€ ์•Š์•„ ๋ธŒ๋ผ์šฐ์ €๊ฐ„์˜ ๋ถˆ์ผ์น˜๊ฐ€ ๋ฐœ์ƒ
  • IE ์ง€์›.

(1) XMLHttpRequest ํ™œ์šฉ

  1. DirectoryHTML ๋ฌธ์„œ์™€ ๊ฐ™์€ ๋ฆฌ์†Œ์Šค๋“ค์„ ๊ฐ€์ ธ์˜ฌ ์ˆ˜ ์žˆ๋„๋ก ํ•ด์ฃผ๋Š” ย ํ”„๋กœํ† ์ฝœHTML ๋ฌธ์„œ์™€ ๊ฐ™์€ ๋ฆฌ์†Œ์Šค๋“ค์„ ๊ฐ€์ ธ์˜ฌ ์ˆ˜ ์žˆ๋„๋ก ํ•ด์ฃผ๋Š” ย ํ”„๋กœํ† ์ฝœ

directory.png


  1. hello.html
<!-- hello.html -->

<h1 class="text">์•ˆ๋…•ํ•˜์„ธ์š”</h1>
<h2 class="text">Ajax ์ž…๋‹ˆ๋‹ค.</h2>

  1. index.html (๋ฌธ์„œ ๋ถˆ๋Ÿฌ์˜ค๊ธฐ๋ฅผ ํด๋ฆญํ–ˆ์„๋•Œ ์š”์ฒญ)
<!-- index.html -->

**<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <title>Document</title>
    <link rel="stylesheet" href="css/reset.css" />
    <style>
      body {
        margin: 30px;
      }
      .box {
        width: 400px;
        padding: 20px;
        border: 1px solid #000;
        margin-top: 20px;
      }
      .box h1 {
        font-size: 30px;
        color: red;
      }
      .box h2 {
        font-size: 25px;
        color: green;
      }
      .box p {
        font-size: 14px;
        color: #999;
      }
    </style>
  </head>
  <body>
    <button class="btn">๋ฌธ์„œ๋ถˆ๋Ÿฌ์˜ค๊ธฐ</button>
    <div class="box">
      <p>๋ฐ์ดํ„ฐ๊ฐ€ ๋“ค์–ด์˜ด</p>
    </div>
    <script>
      const btn = document.querySelector(".btn");
      const box = document.querySelector(".box");
      btn.addEventListener("click", (e) => {
        e.preventDefault();
        const xhr = new XMLHttpRequest();
        const method = "GET";
        const url = "backend/hello.html";

        xhr.onreadystatechange = (e) => {
          console.log(e);
          const { target } = e;

          if ((target.readyState = XMLHttpRequest.DONE)) {
            //Ajax ์ฒ˜๋ฆฌ ๊ฒฐ๊ณผ๋ฅผ ๊ตฌํ˜„ํ•˜๋Š” ์œ„์น˜
            if (target.status == 200) {
              alert("dd");
              const req = target.responseText;
              box.insertAdjacentHTML("beforeend", req);
            } else {
              const a = parseInt(target.status / 100);
              if (a == 4) {
                console.log(
                  "์š”์ฒญ ์ฃผ์†Œ๊ฐ€ ์ž˜๋ชป๋˜์—ˆ์Šต๋‹ˆ๋‹ค",
                  target.status,
                  target.statusText
                );
              } else if (a == 5) {
                console.log(
                  "์„œ๋ฒ„ ์‘๋‹ต์ด ์—†์Œ",
                  target.status,
                  target.statusText
                );
              } else {
                console.log("์š”์ฒญ์— ์‹คํŒจ", target.status, target.statusText);
              }
            }
          }
        };

        xhr.open(method, url);
        xhr.send();
      });
    </script>
  </body>
</html>
**

v1.png


  1. ์š”์ฒญ ๊ฒฐ๊ณผ๊ฐ’

xmlhttp.png

result.png


2) Fetch

[Fetch API - Web APIMDN](https://developer.mozilla.org/ko/docs/Web/API/Fetch_API)

ํŠน์ง•

  • promise ๊ธฐ๋ฐ˜
  • ์ด๋ฏธ ์ž˜ ๋ช…์„ธ๋œ API๊ฐ€ ์ œ๊ณต๋จ์œผ๋กœ์จ ๋ธŒ๋ผ์šฐ์ €๊ฐ„์˜ ๋ถˆ์ผ์น˜๊ฐ€ ์—†์Œ
  • IE ๋Œ€๋ถ€๋ถ„์—์„œ ๋™์ž‘ํ•˜์ง€ ์•Š์Œ

(1) Fetch ํ™œ์šฉ

  1. Directory

directory.png


  1. hello.html
<!-- hello.html -->

<h1 class="text">์•ˆ๋…•ํ•˜์„ธ์š”</h1>
<h2 class="text">Ajax ์ž…๋‹ˆ๋‹ค.</h2>

  1. index.html (๋ฌธ์„œ ๋ถˆ๋Ÿฌ์˜ค๊ธฐ๋ฅผ ํด๋ฆญํ–ˆ์„๋•Œ ์š”์ฒญ)
<!-- index.html -->

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <title>Document</title>
    <link rel="stylesheet" href="css/reset.css" />
    <style>
      body {
        margin: 30px;
      }
      .box {
        width: 400px;
        padding: 20px;
        border: 1px solid #000;
        margin-top: 20px;
      }
      .box h1 {
        font-size: 30px;
        color: red;
      }
      .box h2 {
        font-size: 25px;
        color: green;
      }
      .box p {
        font-size: 14px;
        color: #999;
      }
    </style>
  </head>
  <body>
    <button class="btn">๋ฌธ์„œ๋ถˆ๋Ÿฌ์˜ค๊ธฐ</button>
    <div class="box">
      <p>๋ฐ์ดํ„ฐ๊ฐ€ ๋“ค์–ด์˜ด</p>
    </div>
    <script>
      const btn = document.querySelector(".btn");
      const box = document.querySelector(".box");

      btn.addEventListener("click", (e) => {
        const url = "backend/hello.html";
        fetch(url).then((res) => {
          console.log(res);
          res.text().then((text) => {
            box.insertAdjacentHTML("beforeend", text);
          });
        });
      });
    </script>
  </body>
</html>

v1.png


  1. ์š”์ฒญ ๊ฒฐ๊ณผ๊ฐ’

fetch.png

result.png


3) Axios

[์‹œ์ž‘ํ•˜๊ธฐย Axios Docs](https://axios-http.com/kr/docs/intro)

ํŠน์ง•

  • ๋ธŒ๋ผ์šฐ์ €๋ฅผ ์œ„ํ•ดย XMLHttpRequestsย ์ƒ์„ฑ
  • node.js๋ฅผ ์œ„ํ•ดย httpย ์š”์ฒญ ์ƒ์„ฑ
  • Promiseย API๋ฅผ ์ง€์›
  • ์š”์ฒญ ๋ฐ ์‘๋‹ต ์ธํ„ฐ์…‰ํŠธ
  • ์š”์ฒญ ๋ฐ ์‘๋‹ต ๋ฐ์ดํ„ฐ ๋ณ€ํ™˜
  • ์š”์ฒญ ์ทจ์†Œ
  • JSON ๋ฐ์ดํ„ฐ ์ž๋™ ๋ณ€ํ™˜
  • XSRF๋ฅผ ๋ง‰๊ธฐ์œ„ํ•œ ํด๋ผ์ด์–ธํŠธ ์‚ฌ์ด๋“œ ์ง€์›
  • ํฌ๋กœ์Šค ๋ธŒ๋ผ์šฐ์ง•์— ์‹ ๊ฒฝ์„ ๋งŽ์ด์ผ๊ธฐ์— ๋ธŒ๋ผ์šฐ์ € ํ˜ธํ™˜์„ฑ์ด ์ข‹๋‹ค
  • IE ์ง€์›.

Axios ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ๊ณต์‹ ๋ฌธ์„œ

Axios ๋Ÿฌ๋‹ ๊ฐ€์ด๋“œ

์„ค์น˜

npm ์‚ฌ์šฉํ•˜๊ธฐ:

$npm install axios

bower ์‚ฌ์šฉํ•˜๊ธฐ:

$ bower install axios

yarn ์‚ฌ์šฉํ•˜๊ธฐ:

$yarn add axios

jsDelivr CDN ์‚ฌ์šฉํ•˜๊ธฐ:

<script src="https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js"></script>

unpkg CDN ์‚ฌ์šฉํ•˜๊ธฐ:

<script src="https://unpkg.com/axios/dist/axios.min.js"></script>

(1) Axios ํ™œ์šฉ

  1. Directory

directory.png


  1. hello.html
<!-- hello.html -->

<h1 class="text">์•ˆ๋…•ํ•˜์„ธ์š”</h1>
<h2 class="text">Ajax ์ž…๋‹ˆ๋‹ค.</h2>

  1. index.html (๋ฌธ์„œ ๋ถˆ๋Ÿฌ์˜ค๊ธฐ๋ฅผ ํด๋ฆญํ–ˆ์„๋•Œ ์š”์ฒญ)
<!-- index.html -->

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <title>Document</title>
    <link rel="stylesheet" href="css/reset.css" />
    <script src="https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js"></script>
    <style>
      body {
        margin: 30px;
      }
      .box {
        width: 400px;
        padding: 20px;
        border: 1px solid #000;
        margin-top: 20px;
      }
      .box h1 {
        font-size: 30px;
        color: red;
      }
      .box h2 {
        font-size: 25px;
        color: green;
      }
      .box p {
        font-size: 14px;
        color: #999;
      }
    </style>
  </head>
  <body>
    <button class="btn">๋ฌธ์„œ๋ถˆ๋Ÿฌ์˜ค๊ธฐ</button>
    <div class="box">
      <p>๋ฐ์ดํ„ฐ๊ฐ€ ๋“ค์–ด์˜ด</p>
    </div>
    <script>
      const btn = document.querySelector(".btn");
      const box = document.querySelector(".box");

      btn.addEventListener("click", (e) => {
        ///xxx.data: ์ •๋ณด๊ฐ€ ๋“ค์–ด๊ฐ€ ์žˆ์Œ jsonํŒŒ์ผ์„ ๋ถˆ๋Ÿฌ์˜ค๋ฉด => ๊ฐ์ฒด๋กœ ๊ฐ€์ ธ์˜ด
        const url = "backend/hello.html";
        // axios.get(url).then(res=>console.log(res.data))
        axios
          .get(url)
          .then((res) => {
            console.log(res);
            box.insertAdjacentHTML("beforeend", res.data);
          })
          .catch((err) => {
            console.log("ํŒŒ์ผ์—ฐ๊ฒฐ ์—๋Ÿฌ");
            console.log(err.res.status);
            console.log(err.res.data);
          })
          .finally(() => {
            console.log("๋ฐ์ดํ„ฐ ๋ถˆ๋Ÿฌ์˜ค๊ธฐ ์™„๋ฃŒ");
          });
      });
    </script>
  </body>
</html>

v1.png


  1. ์š”์ฒญ ๊ฒฐ๊ณผ๊ฐ’

axios.png

result.png