Language/JavaScript (WEB)

๐ŸŒ Fetch API ์œผ๋กœ AJAX ์š”์ฒญํ•˜๊ธฐ

์ธํŒŒ_ 2021. 9. 21. 23:54

fetch-api

์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ AJAX ์š”์ฒญ ๋ฐฉ์‹

์ •ํ†ต์ ์œผ๋กœ XMLHttpRequest() ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•˜์—ฌ ์š”์ฒญํ•˜๋Š” ๋ฐฉ๋ฒ•์ด ์žˆ์ง€๋งŒ ๋ฌธ๋ฒ•์ด ๋‚œํ•ดํ•˜๊ณ  ๊ฐ€๋…์„ฑ๋„ ์ข‹์ง€ ์•Š๋‹ค.  ๋”ฐ๋ผ์„œ ์ด๋ฒˆ์‹œ๊ฐ„์—๋Š” ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ AJAX ํ†ต์‹ ์˜ ์ตœ์‹  ๊ธฐ์ˆ ์ธ fetch() ๋ฉ”์„œ๋“œ ์‚ฌ์šฉ๋ฒ•์— ๋Œ€ํ•ด ์•Œ์•„๋ณด๋Š” ์‹œ๊ฐ„์„ ๊ฐ€์ ธ๋ณผ ์˜ˆ์ •์ด๋‹ค.

 

XML Http Request ๋ฐฉ์‹

xmlhttprequest ๊ฐ์ฒด๋ฅผ ์ด์šฉํ•œ ์ •ํ†ต์ ์ธ ์ดˆ์ฐฝ๊ธฐ ๋น„๋™๊ธฐ ์„œ๋ฒ„ ์š”์ฒญ ๋ฐฉ์‹์ด๋‹ค. ์„ฑ๋Šฅ์—๋Š” ๋ฌธ์ œ๋Š” ์—†์ง€๋งŒ ์ฝ”๋“œ๊ฐ€ ๋ณต์žกํ•˜๊ณ  ๊ฐ€๋…์„ฑ์ด ์ข‹์ง€ ์•Š๋‹ค๋Š” ๋‹จ์ ์ด ์žˆ์—ˆ๋‹ค.

var httpRequest = new XMLHttpRequest();

httpRequest.onreadystatechange = function () {
   if (httpRequest.readyState == XMLHttpRequest.DONE && httpRequest.status == 200) {
     	document.getElementById("text").innerHTML = httpRequest.responseText;
   }
}

httpRequest.open("GET", "ajax_intro_data.txt", true);
httpRequest.send();
 

[JS][AJAX] ๐Ÿ“š ์„œ๋ฒ„ ์š”์ฒญ ๋ฐ ์‘๋‹ต (XMLHttpRequest ๋ฐฉ์‹)

์˜›๋‚  ๋ฐฉ์‹์œผ๋กœ Ajax๋ฅผ ๊ตฌํ˜„ํ•˜๋ ค๊ณ  ํ•˜๋ฉด ์ฝ”๋“œ๋„ ๊ธธ๊ณ  ๊นŒ๋‹ค๋กญ์Šต๋‹ˆ๋‹ค. ๊ทธ๋ž˜์„œ ๋น„๊ต์  ๊ตฌํ˜„์ด ๊ฐ„๋‹จํ•œ jQuery๋ฐฉ์‹์„ ์ผ์—ˆ๋Š”๋ฐ, ES6์—์„œ fetch๋ผ๋Š” api๊ฐ€ ๋“ฑ์žฅํ•ด ์š”์ฆ˜์€ ๋‹ค fetch์”๋‹ˆ๋‹ค. โ€‹ ํ•œ ๋งˆ๋””๋กœ ๋‹ค์Œ ์ด

inpa.tistory.com


Fetch API ๋ฐฉ์‹

์ด๋ฒคํŠธ ๊ธฐ๋ฐ˜์ธ XMLHttpRequest๊ณผ๋Š” ๋‹ฌ๋ฆฌ fetch API๋Š” Promise ๊ธฐ๋ฐ˜์œผ๋กœ ๊ตฌ์„ฑ๋˜์–ด ์žˆ์–ด ๋น„๋™๊ธฐ ์ฒ˜๋ฆฌ ํ”„๋กœ๊ทธ๋ž˜๋ฐ ๋ฐฉ์‹์— ์ž˜ ๋งž๋Š” ํ˜•ํƒœ์ด๋‹ค. ๊ทธ๋ž˜์„œ then์ด๋‚˜ catch ์™€ ๊ฐ™์€ ์ฒด์ด๋‹์œผ๋กœ ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ๋‹ค๋Š” ์žฅ์ ์ด ์žˆ๋‹ค. ๋˜ํ•œ fetch API๋Š” JS ๊ธฐ๋ณธ ๊ธฐ๋Šฅ ์ด๊ธฐ ๋•Œ๋ฌธ์—, JQuery์™€ ๊ฐ™์ด CDN ๊ณผ ๊ฐ™์€ ๋‹ค๋ฅธ ์ž‘์—…์„ ํ•˜์ง€ ์•Š์•„๋„ ๋ฐ”๋กœ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

fetch('ajax_intro_data.txt')
    .then( response => response.text() )
    .then( text => { document.getElementById("#t").innerHTML = text; } )

/* 'fetch('์„œ๋ฒ„์ฃผ์†Œ')' ๋Š” ์›น ๋ธŒ๋ผ์šฐ์ €์—๊ฒŒ '์ด ์„œ๋ฒ„์ฃผ์†Œ๋กœ ์š”์ฒญํ•ด์ค˜' ๋ผ๋Š” ์˜๋ฏธ์ด๊ณ , 
๋’ค์— .then์ด ๋ถ™์œผ๋ฉด '์š”์ฒญ ๋๋‚˜๊ณ ๋‚˜์„œ ์ด ํ• ์ผ์„ ํ•ด์ค˜!' ๋ผ๋Š” ๊ฒƒ์ด๋‹ค. */

fetch ๋ฌธ๋ฒ• ์‚ฌ์šฉ๋ฒ•

fetch("https://jsonplaceholder.typicode.com/posts", option)
   .then(res => res.text())
   .then(text => console.log(text));
  1. fetch ์—๋Š” ๊ธฐ๋ณธ์ ์œผ๋กœ ์ฒซ ๋ฒˆ์งธ ์ธ์ž์— ์š”์ฒญํ•  url์ด ๋“ค์–ด๊ฐ„๋‹ค.
  2. ๊ธฐ๋ณธ์ ์œผ๋กœ http ๋ฉ”์†Œ๋“œ ์ค‘ GET์œผ๋กœ ๋™์ž‘ํ•œ๋‹ค.
  3. fetch๋ฅผ ํ†ตํ•ด ajax๋ฅผ ํ˜ธ์ถœ ์‹œ ํ•ด๋‹น ์ฃผ์†Œ์— ์š”์ฒญ์„ ๋ณด๋‚ธ ๋‹ค์Œ, ์‘๋‹ต ๊ฐ์ฒด(Promise object Response)๋ฅผ ๋ฐ›๋Š”๋‹ค.
  4. ๊ทธ๋Ÿฌ๋ฉด ์ฒซ ๋ฒˆ์งธ then์—์„œ ๊ทธ ์‘๋‹ต์„ ๋ฐ›๊ฒŒ๋˜๊ณ , res.text() ๋ฉ”์„œ๋“œ๋กœ ํŒŒ์‹ฑํ•œ text๊ฐ’์„ ๋ฆฌํ„ดํ•œ๋‹ค.
  5. ๊ทธ๋Ÿฌ๋ฉด ๊ทธ ๋‹ค์Œ then์—์„œ ๋ฆฌํ„ด๋ฐ›์€ text ๊ฐ’์„ ๋ฐ›๊ณ , ์›ํ•˜๋Š” ์ฒ˜๋ฆฌ๋ฅผ ํ•  ์ˆ˜ ์žˆ๊ฒŒ ๋œ๋‹ค.

๊ฐœ๋ฐœ์ž ๋„๊ตฌ์˜ ๋„คํŠธ์›Œํฌ ํƒญ์— ๊ฐ€๋ณด๋ฉด fetch๋ฅผ ํ†ตํ•ด ์–ป์–ด์˜จ ๋ฐ์ดํ„ฐ๋ฅผ ๋ณผ์ˆ˜ ์žˆ๋‹ค.

fetch-api


fetch์˜ response ์†์„ฑ

fetch๋ฅผ ํ†ตํ•ด ์š”์ฒญ์„ ํ•˜๊ณ  ์„œ๋ฒ„๋กœ๋ถ€ํ„ฐ ๊ฐ’์„ ์‘๋‹ต ๋ฐ›์œผ๋ฉด .then์„ ํ†ตํ•ด ํ•จ์ˆ˜์˜ ์ธ์ž์— ๋‹ด๊ธฐ๊ฒŒ ๋˜๋Š”๋ฐ ์ด ๊ฐ’์€ Response๊ฐ์ฒด๋กœ์„œ ์—ฌ๋Ÿฌ๊ฐ€์ง€ ์ •๋ณด๋ฅผ ๋‹ด๊ณ  ์žˆ๋‹ค.

ํ•„์š”ํ•œ ๋ณ€์ˆ˜๊ฐ’์ด๋‚˜ ๋ฉ”์„œ๋“œ๋ฅผ ๋ฝ‘์•„๋‚ด์„œ ๊ฐ’์„ ์–ป์œผ๋ฉด ๋œ๋‹ค.

fetch-api

  • response.status – HTTP ์ƒํƒœ ์ฝ”๋“œ(์˜ˆ: 200)
  • response.ok – HTTP ์ƒํƒœ ์ฝ”๋“œ๊ฐ€ 200๊ณผ 299 ์‚ฌ์ด์ผ ๊ฒฝ์šฐ true
  • response.body ใ…ก ๋‚ด์šฉ
  • response.text() – ์‘๋‹ต์„ ์ฝ๊ณ  ํ…์ŠคํŠธ๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค,
  • response.json() – ์‘๋‹ต์„ JSON ํ˜•ํƒœ๋กœ ํŒŒ์‹ฑํ•œ๋‹ค,
  • response.formData() – ์‘๋‹ต์„ FormData ๊ฐ์ฒด ํ˜•ํƒœ๋กœ ๋ฐ˜ํ™˜ํ•œ๋‹ค.
  • response.blob() – ์‘๋‹ต์„ Blob(ํƒ€์ž…์ด ์žˆ๋Š” ๋ฐ”์ด๋„ˆ๋ฆฌ ๋ฐ์ดํ„ฐ) ํ˜•ํƒœ๋กœ ๋ฐ˜ํ™˜ํ•œ๋‹ค.
  • response.arrayBuffer() – ์‘๋‹ต์„ ArrayBuffer(๋ฐ”์ด๋„ˆ๋ฆฌ ๋ฐ์ดํ„ฐ๋ฅผ ๋กœ์šฐ ๋ ˆ๋ฒจ ํ˜•์‹์œผ๋กœ ํ‘œํ˜„ํ•œ ๊ฒƒ) ํ˜•ํƒœ๋กœ ๋ฐ˜ํ™˜ํ•œ๋‹ค.
โš ๏ธ ์ฃผ์˜
์‘๋‹ต ์ž๋ฃŒ ํ˜•ํƒœ ๋ฐ˜ํ™˜ ๋ฉ”์„œ๋“œ๋Š” ํ•œ๋ฒˆ๋งŒ ์‚ฌ์šฉ ํ•  ์ˆ˜ ์žˆ๋‹ค.
๋งŒ์ผ response.text()๋ฅผ ์‚ฌ์šฉํ•ด ์‘๋‹ต์„ ์–ป์—ˆ๋‹ค๋ฉด ๋ณธ๋ฌธ์˜ ์ฝ˜ํ…์ธ ๋Š” ๋ชจ๋‘ ์ฒ˜๋ฆฌ ๋œ ์ƒํƒœ์ด๊ธฐ ๋•Œ๋ฌธ์— ๋’ค์— ๋˜ response.json() ์จ์ค˜๋„ ๋™์ž‘ํ•˜์ง€ ์•Š๊ฒŒ ๋œ๋‹ค.

Fetch - CRUD ์š”์ฒญํ•˜๊ธฐ

http ์š”์ฒญ์—๋Š” get, post ๋ฟ๋งŒ ์•„๋‹ˆ๋ผ put, delete ๊ฐ™์€ ์—ฌ๋Ÿฌ๊ฐ€์ง€๊ฐ€ ์žˆ๋‹ค.

fetch() ๋ฉ”์†Œ๋“œ๋กœ ์–ด๋–ป๊ฒŒ http ์š”์ฒญ์ด๋ž‘ ์กฐํ•ฉํ•ด์„œ ์‚ฌ์šฉํ•˜๋Š”์ง€ ๊ทธ๋ฆฌ๊ณ  ๊ทธ ๋ฌธ๋ฒ•์„ ์•Œ์•„๋ณด๋Š” ์‹œ๊ฐ„์„ ๊ฐ€์ ธ๋ณด์ž.

 

HTTP Method ์ข…๋ฅ˜ (CRUD)

METHOD ์—ญํ• 
POST POST๋ฅผ ํ†ตํ•ด ํ•ด๋‹น URI๋ฅผ ์š”์ฒญํ•˜๋ฉด ๋ฆฌ์†Œ์Šค๋ฅผ ์ƒ์„ฑ
GET GET๋ฅผ ํ†ตํ•ด ํ•ด๋‹น ๋ฆฌ์†Œ์Šค๋ฅผ ์กฐํšŒํ•ฉ๋‹ˆ๋‹ค.
๋ฆฌ์†Œ์Šค๋ฅผ ์กฐํšŒํ•˜๊ณ  ํ•ด๋‹น ๋„ํ๋จผํŠธ์— ๋Œ€ํ•œ ์ž์„ธํ•œ ์ •๋ณด๋ฅผ ๊ฐ€์ ธ
PUT PUT๋ฅผ ํ†ตํ•ด ํ•ด๋‹น ๋ฆฌ์†Œ์Šค๋ฅผ ์ˆ˜์ •
DELETE DELETE๋ฅผ ํ†ตํ•ด ๋ฆฌ์†Œ์Šค๋ฅผ ์‚ญ์ œ

โ€‹

fetch - GET Method

GET : ์กด์žฌํ•˜๋Š” ์ž์›์„ ์š”์ฒญ

  → ๋‹จ์ˆœํžˆ ์›๊ฒฉ API์— ์žˆ๋Š” ๋ฐ์ดํ„ฐ๋ฅผ ๊ฐ€์ ธ์˜ฌ ๋•Œ ์“ฐ์ž„

  → fetchํ•จ์ˆ˜๋Š” ๋””ํดํŠธ๋กœ GET ๋ฐฉ์‹์œผ๋กœ ์ž‘๋™ํ•˜๊ณ , ์˜ต์…˜ ์ธ์ž๊ฐ€ ํ•„์š” ์—†์Œ.

  → ์‘๋‹ต(response) ๊ฐ์ฒด๋Š” json() ๋ฉ”์„œ๋“œ๋ฅผ ์ œ๊ณตํ•˜๊ณ , ์ด ๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœํ•˜๋ฉด ์‘๋‹ต(response) ๊ฐ์ฒด๋กœ๋ถ€ํ„ฐ JSON ํ˜•ํƒœ์˜ ๋ฐ์ดํ„ฐ๋ฅผ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ๊ฐ์ฒด๋กœ ๋ณ€ํ™˜ํ•˜์—ฌ ์–ป์„ ์ˆ˜ ์žˆ์Œ.

fetch("https://jsonplaceholder.typicode.com/posts/1") // posts์˜ id 1์ธ ์—˜๋ฆฌ๋จผํŠธ๋ฅผ ๊ฐ€์ ธ์˜ด 
  .then((response) => response.json())
  .then((data) => console.log(data))
{
  "userId": 1,
  "id": 1,
  "title": "sunt aut facere repellat provident occaecati excepturi optio reprehenderit",
  "body": "quia et suscipit↵suscipit recusandae consequuntur …strum rerum est autem sunt rem eveniet architecto"
}

 

fetch - POST Method

POST : ์ƒˆ๋กœ์šด ์ž์› ์ƒ์„ฑ ์š”์ฒญ

  → ํผ ๋“ฑ์„ ์‚ฌ์šฉํ•ด์„œ ๋ฐ์ดํ„ฐ๋ฅผ ๋งŒ๋“ค์–ด ๋‚ผ ๋•Œ, ๋ณด๋‚ด๋Š” ๋ฐ์ดํ„ฐ์˜ ์–‘์ด ๋งŽ๊ฑฐ๋‚˜, ๋น„๋ฐ€๋ฒˆํ˜ธ ๋“ฑ ๊ฐœ์ธ์ •๋ณด๋ฅผ ๋ณด๋‚ผ ๋•Œ POST ๋ฉ”์„œ๋“œ ์‚ฌ์šฉ

  → ์ƒˆ๋กœ์šด ํฌ์ŠคํŠธ ์ƒ์„ฑ ์œ„ํ•ด์„œ๋Š” method ์˜ต์…˜์„ POST๋กœ ์ง€์ •ํ•ด์ฃผ๊ณ , headers ์˜ต์…˜์œผ๋กœ JSON ํฌ๋งท ์‚ฌ์šฉํ•œ๋‹ค๊ณ  ์•Œ๋ ค์ค˜์•ผ ํ•จ. body ์˜ต์…˜์—๋Š” ์š”์ฒญ ๋ฐ์ดํ„ฐ๋ฅผ JSON ํฌ๋งท์œผ๋กœ ๋„ฃ์–ด์คŒ.

  • method – HTTP ๋ฉ”์„œ๋“œ
  • headers – ์š”์ฒญ ํ—ค๋“œ๊ฐ€ ๋‹ด๊ธด ๊ฐ์ฒด(์ œ์•ฝ ์‚ฌํ•ญ์ด ์žˆ์Œ)
  • body – ๋ณด๋‚ด๋ ค๋Š” ๋ฐ์ดํ„ฐ(์š”์ฒญ ๋ณธ๋ฌธ)๋กœ string์ด๋‚˜ FormData, BufferSource, Blob, UrlSearchParams ๊ฐ์ฒด ํ˜•ํƒœ
fetch("https://jsonplaceholder.typicode.com/posts", {
  method: "POST", // POST
  headers: { // ํ—ค๋” ์กฐ์ž‘
    "Content-Type": "application/json",
  },
  body: JSON.stringify({ // ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ๊ฐ์ฒด๋ฅผ jsonํ™” ํ•œ๋‹ค.
    title: "Test",
    body: "I am testing!",
    userId: 1,
  }),
})
  .then((response) => response.json())
  .then((data) => console.log(data))

โ€‹

fetch - PUT Method (์ „์ฒด์ˆ˜์ •)

PUT : ์กด์žฌํ•˜๋Š” ์ž์› ๋ณ€๊ฒฝ ์š”์ฒญ

  → API์—์„œ ๊ด€๋ฆฌํ•˜๋Š” ๋ฐ์ดํ„ฐ์˜ ์ˆ˜์ •์„ ์œ„ํ•ด PUT ๋ฉ”์„œ๋“œ ์‚ฌ์šฉํ•จ.

  → method ์˜ต์…˜๋งŒ PUT์œผ๋กœ ์„ค์ •ํ•œ๋‹ค๋Š” ์  ๋นผ๋†“๊ณ ๋Š” POST ๋ฐฉ์‹๊ณผ ๋น„์Šท

  → ์•„์˜ˆ ์ „์ฒด๋ฅผ body์˜ ๋ฐ์ดํ„ฐ๋กœ ๊ต์ฒดํ•ด๋ฒ„๋ฆผ.

fetch("https://jsonplaceholder.typicode.com/posts", {
  method: "PUT",
  headers: {
    "Content-Type": "application/json",
  },
  body: JSON.stringify({
    title: "Test" // ์•„์˜ˆ title ์—˜๋ฆฌ๋จผํŠธ๋กœ ์ „์ฒด ๋ฐ์ดํ„ฐ๋ฅผ ๋ฐ”๊ฟˆ. ๋งˆ์น˜ innerHTML๊ฐ™์ด.
  }),
})
  .then((response) => response.json())
  .then((data) => console.log(data))

โ€‹

fetch - PATCH Method (๋ถ€๋ถ„์ˆ˜์ •)

PATCH : ์กด์žฌํ•˜๋Š” ์ž์› ์ผ๋ถ€ ๋ณ€๊ฒฝ ์š”์ฒญ

  → body์˜ ๋ฐ์ดํ„ฐ์™€ ์•Œ๋งž๋Š” ์ผ๋ถ€๋งŒ์„ ๊ต์ฒดํ•จ .

fetch("https://jsonplaceholder.typicode.com/posts/1", { // posts์˜ id 1์ธ ์—˜๋ฆฌ๋จผํŠธ๋ฅผ ์ˆ˜์ •
  method: "PATCH",
  headers: {
    "Content-Type": "application/json",
  },
  body: JSON.stringify({
    title: "Test" // title๋งŒ ๋ฐ”๊ฟˆ. ๋‚˜๋จธ์ง€ ์š”์†Œ๋Š” ๊ฑด๋“ค์ง€ ์•Š์Œ.
  }),
})
  .then((response) => response.json())
  .then((data) => console.log(data))

โ€‹

fetch - DELETE Method

DELETE : ์กด์žฌํ•˜๋Š” ์ž์› ์‚ญ์ œ ์š”์ฒญ

  → ๋ณด๋‚ผ ๋ฐ์ดํ„ฐ๊ฐ€ ์—†๊ธฐ ๋•Œ๋ฌธ์— headers, body ์˜ต์…˜์ด ํ•„์š”์—†์Œ

fetch("https://jsonplaceholder.typicode.com/posts/1", {
  method: "DELETE",
})
  .then((response) => response.json())
  .then((data) => console.log(data))

 


Fetch - async / await ๋ฌธ๋ฒ•

fetch์˜ ๋ฆฌํ„ด๊ฐ’ response๋Š” Promise๊ฐ์ฒด ์ด๋‹ค. ๋‹น์—ฐํžˆ await / async ๋ฌธ๋ฒ•์œผ๋กœ ๋ณด๋‹ค ๊ฐ€๋…์„ฑ์„ ๋†’์ด๊ฒŒ ์ฝ”๋”ฉ ํ•  ์ˆ˜ ์žˆ๋‹ค.

fetch("https://jsonplaceholder.typicode.com/posts", option)
.then(res => res.text())
.then(text => console.log(text));
(async () => {
  let res = await fetch("https://jsonplaceholder.typicode.com/posts", option);
  let text = await res.text();
  console.log(text);
})() //await์€ asyncํ•จ์ˆ˜๋‚ด์—์„œ๋งŒ ์“ธ์ˆ˜ ์žˆ์œผ๋‹ˆ, ์ต๋ช… async ๋ฐ”๋กœ ์‹คํ–‰ํ•จ์ˆ˜๋ฅผ ํ†ตํ•ด ํ™œ์šฉํ•ฉ๋‹ˆ๋‹ค.
 

[JS] ๐Ÿ“š ๋น„๋™๊ธฐ์ฒ˜๋ฆฌ (async / await) ๊ฐœ๋… & ๋ฌธ๋ฒ• ๐Ÿ’ฏ ์ •๋ฆฌ

๋น„๋™๊ธฐ ์ฒ˜๋ฆฌ ๋ฐฉ์‹ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ๋Š” ์‹ฑ๊ธ€ ์Šค๋ ˆ๋“œ ํ”„๋กœ๊ทธ๋ž˜๋ฐ์–ธ์–ด๊ธฐ ๋•Œ๋ฌธ์— ๋น„๋™๊ธฐ์ฒ˜๋ฆฌ๊ฐ€ ํ•„์ˆ˜์ ์ด๋‹ค. ๋น„๋™๊ธฐ ์ฒ˜๋ฆฌ๋Š” ๊ทธ ๊ฒฐ๊ณผ๊ฐ€ ์–ธ์ œ ๋ฐ˜ํ™˜๋ ์ง€ ์•Œ์ˆ˜ ์—†๊ธฐ ๋•Œ๋ฌธ์— ๋™๊ธฐ์‹์œผ๋กœ ์ฒ˜๋ฆฌํ•˜๋Š” ๊ธฐ๋ฒ•๋“ค์ด

inpa.tistory.com


fetch ๋ชจ๋“ˆํ™” - ์‚ฌ์šฉ์„ฑ ๊ฐœ์„ ํ•˜๊ธฐ

fetch() ํ•จ์ˆ˜๋Š” ์‚ฌ์šฉ๋ฒ•์ด ์•„์ฃผ ๊ฐ„๋‹จํ•˜์ง€๋งŒ, ๊ณ„์† ์‚ฌ์šฉํ•˜๋‹ค๋ณด๋ฉด ๋˜‘๊ฐ™์€ ์ฝ”๋“œ๊ฐ€ ๋ฐ˜๋ณต๋œ๋‹ค๋Š” ๊ฒƒ์„ ๋Š๋‚„ ๊ฒƒ์ด๋‹ค.

์˜ˆ๋ฅผ ๋“ค์–ด, ์‘๋‹ต ๋ฐ์ดํ„ฐ์„ ์–ป๊ธฐ ์œ„ํ•ด์„œ response.json()์„ ๋งค๋ฒˆ ํ˜ธ์ถœํ•˜๊ฑฐ๋‚˜, ๋ฐ์ดํ„ฐ๋ฅผ ๋ณด๋‚ผ ๋•Œ, HTTP ์š”์ฒญ ํ—ค๋”์— "Content-Type": "application/json"๋กœ ์ผ์ผํžˆ ์„ค์ •ํ•ด์ค˜์•ผ ๋˜๊ฑฐ๋‚˜ ์จ์•ผ ๋ ๊ฒŒ ๋งŽ๋‹ค.

ํ•œ๋‘๋ฒˆ fetch() ๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ์ •๋„๋Š” ๋ฌธ์ œ๊ฐ€ ์•ˆ๋˜์ง€๋งŒ, ์—ฌ๋Ÿฌ๋ฒˆ ์‚ฌ์šฉํ•  ์ผ์ด ์žˆ์œผ๋ฉด ๋”ฐ๋กœ ํ—ฌํผ ํ•จ์ˆ˜๋ฅผ ๋งŒ๋“ค์–ด ๋ชจ๋“ˆํ™”๋ฅผ ํ•ด๋†“๋Š”๊ฒŒ ๋‚˜์ค‘์— ์ •๋ง ํŽธํ•˜๊ฒŒ ์‚ฌ์šฉํ• ์ˆ˜ ์žˆ๋‹ค.

 

์•„๋ž˜ ์ฝ”๋“œ์™€ ๊ฐ™์ด POST ์š”์ฒญ์„ ๋ณด๋‚ผ ํ•„์š”๊ฐ€ ์žˆ๋‹ค๋ฉด ํ•จ์ˆ˜๋กœ ๋”ฐ๋กœ ๋ฏธ๋ฆฌ ๊ตฌ์„ฑ์„ ์งœ๋†“๊ณ , ์ธ์ž๋กœ ๊ฐ’๋“ค์„ ์ „๋‹ฌํ•ด ๋ฐ”๋กœ๋ฐ”๋กœ ๊ฐ’์„ ์‘๋‹ต ๋ฐ›๋Š” ์‹์œผ๋กœ ๊ณ ๊ธ‰ ๊ตฌ์„ฑ์„ ํ•ด๋†“๋Š”๋‹ค.

async function post(host, path, body, headers = {}) {
  const url = `https://${host}/${path}`;
  const options = {
    method: "POST",
    headers: {
      "Content-Type": "application/json",
      ...headers,
    },
    body: JSON.stringify(body),
  };
  const res = await fetch(url, options);
  const data = await res.json();
  if (res.ok) {
    return data;
  } else {
    throw Error(data);
  }
}

post("jsonplaceholder.typicode.com", "posts", {
  title: "Test",
  body: "I am testing!",
  userId: 1,
})
  .then((data) => console.log(data))
  .catch((error) => console.log(error));

๊ตฌํ˜• ๋ธŒ๋ผ์šฐ์ €์—์„œ fetch ์‚ฌ์šฉํ•˜๊ธฐ

fetch๋Š” ES6 ์ตœ์‹  ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ๊ธฐ์ˆ ์ด๋‹ค. ๋”ฐ๋ผ์„œ ์ธํ„ฐ๋„ท ์ต์Šคํ”Œ๋กœ์–ด ๊ฐ™์€ ๊ตฌํ˜• ๋ธŒ๋ผ์šฐ์ €์—๋Š” ๋™์ž‘์„ ์•ˆํ•˜๋Š”๋ฐ, ์ด๋•Œ๋Š” ๋”ฐ๋กœ polyfill๋œ ์ฝ”๋“œ๋ฅผ ์ ์šฉํ•ด ๊ฐ™์ด ์‚ฌ์šฉํ•˜๋ฉด ๋ฌด๋ฆฌ์—†์ด fetch๋ฅผ ์‚ฌ์šฉ ํ• ์ˆ˜ ์žˆ๋‹ค.

ํด๋ฆฌํ•„(polyfill)์ด๋ผ๋Š” ๋‹จ์–ด๊ฐ€ ๋ญ”๊ฐ€ ์ƒ์†Œํ•ด์„œ ๋ณต์žกํ•˜๊ณ  ์–ด๋ ค์šธ๊ฒƒ ๊ฐ™์•„๋ณด์ด์ง€๋งŒ ์ง์ ‘ํ•ด๋ณด๋ฉด ๋งค์šฐ ๊ฐ„๋‹จํ•˜๋‹ค.

ํด๋ฆฌํ•„(polyfill)์€ ์›น ๊ฐœ๋ฐœ์—์„œ ๊ธฐ๋Šฅ์„ ์ง€์›ํ•˜์ง€ ์•Š๋Š” ์›น ๋ธŒ๋ผ์šฐ์ € ์ƒ์˜ ๊ธฐ๋Šฅ์„ ๊ตฌํ˜„ํ•˜๋Š” ์ฝ”๋“œ๋ฅผ ๋œปํ•œ๋‹ค.
 

GitHub - github/fetch: A window.fetch JavaScript polyfill.

A window.fetch JavaScript polyfill. Contribute to github/fetch development by creating an account on GitHub.

github.com

์œ„ ๋งํฌ์—์„œ fetch.js๋ผ๋Š” ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ๋‹ค์šด๋ฐ›๊ณ , ์•Œ๋งž์€ ๊ฒฝ๋กœ๋กœ src๋ฅผ ์ง€์ •ํ•˜๋ฉด ๋œ๋‹ค. โ€‹์ด๋ ‡๊ฒŒ ํ•ด์ฃผ๋ฉด fetch api๋ฅผ ์ง€์›ํ•˜๋Š” ์ตœ์‹ ๋ธŒ๋ผ์šฐ์ €๋Š” ๊ทธ๋Œ€๋กœ ์ง„ํ–‰ํ•˜๊ณ , ์ง€์›ํ•˜์ง€ ์•Š๋Š” ๊ตฌํ˜• ๋ธŒ๋ผ์šฐ์ €๋Š” fectch.js๋‚ด์— ์žˆ๋Š” ํ•จ์ˆ˜๋ฅผ ํ†ตํ•ด ajax๊ฐ€ ์ง„ํ–‰๋˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.

<head>
  <meta charset="utf-8">
  <script src="../fetch.js"></script>
</head>