java

JSP - AJAXํ†ต์‹  ์ž…๋ฌธํ•˜๊ธฐ

dev_summer 2020. 11. 12. 11:38

 

 

- AJAX(Asynchronous Javascript And XML)

javascript๋ฅผ ์ด์šฉํ•œ ๋น„๋™๊ธฐ ๋ฐ์ดํ„ฐ ํ†ต์‹  ๊ธฐ์ˆ 

๋’ค๋กœ ๊ฐ€๊ธฐ๊ฐ€ ์—†๋Š”(ํŽ˜์ด์ง€ ์ด๋™์ด ์—†๋Š”) ๋ถ€๋ถ„์ ์œผ๋กœ ์ •๋ณด๋ฅผ ๋ฆฌ๋กœ๋“œ ํ•˜๋Š” ๊ธฐ์ˆ 

์‘๋‹ตํ•˜๋Š” ๊ณณ์ด html์ด ์•„๋‹Œ ๋ฐ์ดํ„ฐ๋ฅผ ๋ฆฌํ„ดํ•ด์ฃผ์–ด ๋ถ€ํ•˜๊ฐ€ ํ›จ์”ฌ ์ ๊ณ  ๋น ๋ฅด๋‹ค.

์š”์ฒญํ• ๋•Œ xmlhttprequest, Data๋ฅผ ์š”์ฒญํ•˜๊ณ  data๋ฅผ returnํ•ด์ค€๋‹ค.

๊ทธ ๋ถ€๋ถ„์„ ๋ถ€๋ถ„๋ฆฌ๋กœ๋”ฉํ•ด์ค€๋‹ค.

 

์ฐธ๊ณ ๋กœ ์š”์ฆ˜์€ websoket์‚ฌ์šฉํ•œ๋‹ค :-)

 

 

ํ˜„์žฌ ๋ฐฐ์šฐ๊ณ ์žˆ๋Š” ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ๋ฒ„์ „์€ ES5์ด๋‹ค.

ES6,7๋“ฑ์˜ ์ตœ์‹ ๋ฒ„์ „์˜ ์Šคํฌ๋ฆฝํŠธ๋ฌธ๋ฒ•์€ ๋ธŒ๋ผ์šฐ์ €๊ฐ€ ์ปดํŒŒ์ผ๋Ÿฌ ํ•ด์ฃผ์ง€ ๋ชปํ•ด์„œ ํƒ„์ƒํ•œ ๊ฒƒ์ด ๋ฐ”๋ฒจ์ด๋‹ค.

*๋ฐ”๋ฒจ(babel) : EX6 ๋ฅผ ES5๋กœ ์ปดํŒŒ์ผ ํ•ด์ฃผ๋Š” ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์ปดํŒŒ์ผ๋Ÿฌ

์ด๋Ÿฌํ•œ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ์ปดํŒŒ์ผ๋Ÿฌ์˜ ๋“ฑ์žฅ์œผ๋กœ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์ธ ์ œ์ด์ฟผ๋ฆฌ๋Š” ํ•˜ํ–ฅ์„ ํƒ€๊ณ ์žˆ๊ณ  ์ˆœ์ˆ˜ํ•œ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ๋ฅผ ์ด์šฉํ•˜์—ฌ ๊ฐœ๋ฐœ์„ ํ•˜๊ณ ์žˆ๋‹ค.

 

 

 

<!DOCTYPE html>
<html>
  <body>
    <div id="demo">
      <h2>The XMLHttpRequest Object</h2>
      <button type="button" onclick="loadDoc()">Change Content</button>
      <a href="ajax_info.txt">ํŒŒ์ผ์ด๋™</a>
    </div>

    <script>
      function loadDoc() {
        var responseText = fetch("ajax_info.txt");
        console.log(responseText);
      }
    </script>
  </body>
</html>

 

 

 

 

 

๊ฐœ๋ฐœ์ž๋ชจ๋“œ์—์„œ ajax_info.txt๋ฅผ ์ฐพ์ง€ ๋ชปํ•˜๋Š” ์ด์œ ๋Š” ํŒจ์น˜๋ฅผ ํ•˜๋Š” ์ˆœ๊ฐ„ ํ•˜๋“œ๋””์Šคํฌ์˜ i/o๋ฅผ ํ•œ๋‹ค.

i/oํ•œ ๊ณผ์ •์ค‘์— responseText์— ๋„ฃ๊ธฐ๋•Œ๋ฌธ์— ๋ณด์ด์ง€ ์•Š์Œ.

 

promise : ์–ธ์  ๊ฐ„ ๋“ค์–ด์˜ฌ ๋ฐ์ดํ„ฐ

 

 

 

 

<!DOCTYPE html>
<html>
  <body>
    <div id="demo">
      <h2>The XMLHttpRequest Object</h2>
      <button type="button" onclick="loadDoc()">Change Content</button>
      <a href="ajax_info.txt">ํŒŒ์ผ์ด๋™</a>
    </div>

    <script>
      async function loadDoc() {
        var responseText = await fetch("ajax_info.txt");
        console.log(responseText);
      }
    </script>
  </body>
</html>

 

 

 

 

 

๋ฐ์ดํ„ฐ๋ฅผ ๋ฐ›์Œ.

 

fetch๋ฅผ ํ•˜๊ธฐ ์ „ ๊ธฐ๋‹ค๋ฆฐ ํ›„ ๋ณ€์ˆ˜์— ๋„ฃ๋Š” awaitํ•ด์•ผํ•œ๋‹ค.

 

<!DOCTYPE html>
<html>
  <body>
    <div id="demo">
      <h2>The XMLHttpRequest Object</h2>
      <button type="button" onclick="loadDoc()">Change Content</button>
      <a href="ajax_info.txt">ํŒŒ์ผ์ด๋™</a>
    </div>

    <script>
      async function loadDoc() {
        var response = await fetch("ajax_info.txt");
        var responseText = await response.text(); // ํŒŒ์‹ฑํ•˜๋Š” ์‹œ๊ฐ„๋˜ํ•œ ๊ธฐ๋‹ค๋ ธ๋‹ค๊ฐ€ ๋„ฃ์Œ
        console.log(responseText);
      }
    </script>
  </body>
</html>

 

 

 

 

 

 

๊ธฐ๋‹ค๋ ค์ฃผ๋Š” await๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๊ฒฐ๊ณผ๊ฐ’์„ ์–ป์„ ์ˆ˜ ์žˆ๋‹ค.

 

๋ชจ~ ๋“  ํ†ต์‹ ์€ ๋‹ค ๋ถ™์–ด์•ผ ํ•œ๋‹ค.

 

 

 

<!DOCTYPE html>
<html>
  <body>
    <div id="demo">
      <h2>The XMLHttpRequest Object</h2>
      <button type="button" onclick="loadDoc()">Change Content</button>
      <a href="ajax_info.txt">ํŒŒ์ผ์ด๋™</a>
    </div>

    <script>
      async function loadDoc() {
        var response = await fetch("ajax_info.txt");
        var responseText = await response.json(); // json์„ ํŒŒ์‹ฑํ•ด์„œ ๋ฟŒ๋ ค์คŒ
        console.log(responseText);
        console.log(responseText.name); //.name์ฒ˜๋Ÿผ ์ ‘๊ทผ์ด ๊ฐ€๋Šฅํ•˜๋‹ค. ๊ฒฐ๊ณผ๊ฐ’์€ 
       //var t = JSON.parse(responseText); ์œผ๋กœ ํ•˜๋ฉด t.name์œผ๋กœ ์ ‘๊ทผ์ด ๊ฐ€๋Šฅํ•˜๋‹ค. 
      }
    </script>
  </body>
</html>

 

 

 

 

 

.json์œผ๋กœ ๋ฐ›์œผ๋ฉด ์ปดํŒŒ์ผ ์‹œ ํŒŒ์‹ฑ๋˜์–ด ๊ฐ์ฒด๋กœ ์ ‘๊ทผ์ด ๊ฐ€๋Šฅํ•˜๋‹ค.