ํ‹ฐ์Šคํ† ๋ฆฌ ๋ทฐ

Ajax๋ž€?

Ajax(Asynchronous JavaScript and XML)๋ž€ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋ธŒ๋ผ์šฐ์ €๊ฐ€ ์„œ๋ฒ„์—๊ฒŒ ๋น„๋™๊ธฐ ๋ฐฉ์‹์œผ๋กœ ๋ฐ์ดํ„ฐ๋ฅผ ์š”์ฒญํ•˜๊ณ , ์„œ๋ฒ„๊ฐ€ ์‘๋‹ตํ•œ ๋ฐ์ดํ„ฐ๋ฅผ ์ˆ˜์‹ ํ•˜์—ฌ ์›นํŽ˜์ด์ง€๋ฅผ ๋™์ ์œผ๋กœ ๊ฐฑ์‹ ํ•˜๋Š” ํ”„๋กœ๊ทธ๋ž˜๋ฐ ๋ฐฉ์‹์„ ๋งํ•ฉ๋‹ˆ๋‹ค.

  • ๋ธŒ๋ผ์šฐ์ €์—์„œ ์ œ๊ณตํ•˜๋Š” Web API์ธ XMLHttpRequest ๊ฐ์ฒด๋ฅผ ๊ธฐ๋ฐ˜์œผ๋กœ ๋™์ž‘
  • XMLHttpRequest๋Š” HTTP ๋น„๋™๊ธฐ ํ†ต์‹ ์„ ์œ„ํ•œ ๋ฉ”์„œ๋“œ์™€ ํ”„๋กœํผํ‹ฐ๋ฅผ ์ œ๊ณต

 

์ด์ „์˜ ์›นํŽ˜์ด์ง€๋Š” ์™„์ „ํ•œ HTML์„ ์„œ๋ฒ„๋กœ๋ถ€ํ„ฐ ์ „์†ก๋ฐ›์•„ ์›นํŽ˜์ด์ง€ ์ „์ฒด๋ฅผ ์ฒ˜์Œ๋ถ€ํ„ฐ ๋‹ค์‹œ ๋ Œ๋”๋ง ํ•˜๋Š” ๋ฐฉ์‹์œผ๋กœ, ๋ณ€๊ฒฝํ•  ํ•„์š”๊ฐ€ ์—†๋Š” ๋ถ€๋ถ„๊นŒ์ง€ ๋‹ค์‹œ ๋ Œ๋”๋ง๋˜๋Š” ๋‹จ์ ์ด ์žˆ์—ˆ์Šต๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ Ajax์˜ ๋“ฑ์žฅ์€ ์ด์ „์˜ ์ „ํ†ต์ ์ธ ํŒจ๋Ÿฌ๋‹ค์ž„์„ ํš๊ธฐ์ ์œผ๋กœ ์ „ํ™˜ํ–ˆ์Šต๋‹ˆ๋‹ค. 

 

  • ์„œ๋ฒ„๋กœ๋ถ€ํ„ฐ ์›นํŽ˜์ด์ง€์˜ ๋ณ€๊ฒฝ์— ํ•„์š”ํ•œ ๋ฐ์ดํ„ฐ๋งŒ ์ „์†ก๋ฐ›์•„ ๋ถˆํ•„์š”ํ•œ ๋ฐ์ดํ„ฐ ํ†ต์‹  ๋ฐœ์ƒ X
  • ๋ณ€๊ฒฝํ•  ํ•„์š”๊ฐ€ ์žˆ๋Š” ๋ถ€๋ถ„๋งŒ ํ•œ์ •์ ์œผ๋กœ ๋ Œ๋”๋งํ•ด ํ™”๋ฉด์ด ์ˆœ๊ฐ„์ ์œผ๋กœ ๊นœ๋ฐ•์ด๋Š” ํ˜„์ƒ์ด ๋ฐœ์ƒ X
  • ํด๋ผ์ด์–ธํŠธ์™€ ์„œ๋ฒ„์™€์˜ ํ†ต์‹ ์ด ๋น„๋™๊ธฐ ๋ฐฉ์‹์œผ๋กœ ๋™์ž‘ํ•ด ์„œ๋ฒ„์—์„œ ์š”์ฒญ์„ ๋ณด๋‚ธ ์ดํ›„ ๋ธ”๋กœํ‚น์ด ๋ฐœ์ƒ X
  • ๋ธŒ๋ผ์šฐ์ €์—์„œ๋„ ๋ฐ์Šคํฌํ†ฑ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜๊ณผ ์œ ์‚ฌํ•œ ๋น ๋ฅธ ํผํฌ๋จผ์Šค์™€ ๋ถ€๋“œ๋Ÿฌ์šด ํ™”๋ฉด ์ „ํ™˜์ด ๊ฐ€๋Šฅ

JSON

JSON(JavaScript Object Notation)์€ ํด๋ผ์ด์–ธํŠธ์™€ ์„œ๋ฒ„ ๊ฐ„์˜ HTTP ํ†ต์‹ ์„ ์œ„ํ•œ ํ…์ŠคํŠธ ๋ฐ์ดํ„ฐ ํฌ๋งท์œผ๋กœ, ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์— ์ข…์†๋˜์ง€ ์•Š๋Š” ์–ธ์–ด ๋…๋ฆฝํ˜• ๋ฐ์ดํ„ฐ ํฌ๋งท์ž…๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ ๋Œ€๋ถ€๋ถ„์˜ ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์–ธ์–ด์—์„œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

JSON ํ‘œ๊ธฐ ๋ฐฉ์‹

JSON์€ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์˜ ๊ฐ์ฒด ๋ฆฌํ„ฐ๋Ÿด๊ณผ ์œ ์‚ฌํ•˜๊ฒŒ ํ‚ค์™€ ๊ฐ’์œผ๋กœ ๊ตฌ์„ฑ๋œ ์ˆœ์ˆ˜ํ•œ ํ…์ŠคํŠธ์ž…๋‹ˆ๋‹ค.

 

{
  "name": "Lee",
  "age": 20,
  "alive": true,
  "hobby": ["traveling", "tennis"]
}
  • JSON์˜ ํ‚ค : ๋ฐ˜๋“œ์‹œ ํฐ๋”ฐ์˜ดํ‘œ๋กœ ๋ฌถ์–ด์•ผ ํ•จ
  • JSON์˜ ๊ฐ’ : ๊ฐ์ฒด ๋ฆฌํ„ฐ๋Ÿด๊ณผ ๊ฐ™์€ ํ‘œ๊ธฐ๋ฒ•์„ ๊ทธ๋Œ€๋กœ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์ง€๋งŒ ๋ฌธ์ž์—ด์€ ๋ฐ˜๋“œ์‹œ ํฐ๋”ฐ์˜ดํ‘œ๋กœ ๋ฌถ์–ด์•ผ ํ•จ

 

JSON.stringify

์ง๋ ฌํ™”(serializing)๋ž€ ํด๋ผ์ด์–ธํŠธ๊ฐ€ ์„œ๋ฒ„๋กœ ๊ฐ์ฒด๋ฅผ ์ „์†กํ•˜๊ธฐ ์œ„ํ•ด ๊ฐ์ฒด๋ฅผ ๋ฌธ์ž์—ดํ™”ํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์ด๋•Œ, JSON.stringify

๋ฉ”์„œ๋“œ๋ฅผ ํ†ตํ•ด ๊ฐ์ฒด๋ฅผ JSON ํฌ๋งท์˜ ๋ฌธ์ž์—ด๋กœ ๋ณ€ํ™˜ํ•ฉ๋‹ˆ๋‹ค. ๋˜ํ•œ, JSON.stringify ๋ฉ”์„œ๋“œ๋Š” ๊ฐ์ฒด๋ฟ๋งŒ ์•„๋‹ˆ๋ผ ๋ฐฐ์—ด๋„ JSON ํฌ๋งท์˜ ๋ฌธ์ž์—ด๋กœ ๋ณ€ํ™˜ํ•ฉ๋‹ˆ๋‹ค.

 

// ๊ฐ์ฒด ๋ณ€ํ™˜
const obj = {
  name: 'Lee',
  age: 20,
  alive: true,
  hobby: ['traveling', 'tennis']
};

// ๊ฐ์ฒด๋ฅผ JSON ํฌ๋งท์˜ ๋ฌธ์ž์—ด๋กœ ๋ณ€ํ™˜ํ•œ๋‹ค.
const json = JSON.stringify(obj);
console.log(typeof json, json);
// string {"name":"Lee","age":20,"alive":true,"hobby":["traveling","tennis"]}

// ๊ฐ์ฒด๋ฅผ JSON ํฌ๋งท์˜ ๋ฌธ์ž์—ด๋กœ ๋ณ€ํ™˜ํ•˜๋ฉด์„œ ๋“ค์—ฌ์“ฐ๊ธฐ ํ•œ๋‹ค.
const prettyJson = JSON.stringify(obj, null, 2);
console.log(typeof prettyJson, prettyJson);
/*
string {
  "name": "Lee",
  "age": 20,
  "alive": true,
  "hobby": [
    "traveling",
    "tennis"
  ]
}
*/

// replacer ํ•จ์ˆ˜. ๊ฐ’์˜ ํƒ€์ž…์ด Number์ด๋ฉด ํ•„ํ„ฐ๋ง๋˜์–ด ๋ฐ˜ํ™˜๋˜์ง€ ์•Š๋Š”๋‹ค.
function filter(key, value) {
  // undefined: ๋ฐ˜ํ™˜ํ•˜์ง€ ์•Š์Œ
  return typeof value === 'number' ? undefined : value;
}

// JSON.stringify ๋ฉ”์„œ๋“œ์— ๋‘ ๋ฒˆ์งธ ์ธ์ˆ˜๋กœ replacer ํ•จ์ˆ˜๋ฅผ ์ „๋‹ฌํ•œ๋‹ค.
const strFilteredObject = JSON.stringify(obj, filter, 2);
console.log(typeof strFilteredObject, strFilteredObject);
/*
string {
  "name": "Lee",
  "alive": true,
  "hobby": [
    "traveling",
    "tennis"
  ]
}
*/


// ๋ฐฐ์—ด ๋ณ€ํ™˜
const todos = [
  { id: 1, content: 'HTML', completed: false },
  { id: 2, content: 'CSS', completed: true },
  { id: 3, content: 'Javascript', completed: false }
];

// ๋ฐฐ์—ด์„ JSON ํฌ๋งท์˜ ๋ฌธ์ž์—ด๋กœ ๋ณ€ํ™˜ํ•œ๋‹ค.
const json = JSON.stringify(todos, null, 2);
console.log(typeof json, json);
/*
string [
  {
    "id": 1,
    "content": "HTML",
    "completed": false
  },
  {
    "id": 2,
    "content": "CSS",
    "completed": true
  },
  {
    "id": 3,
    "content": "Javascript",
    "completed": false
  }
]
*/

 

JSON.parse

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

๊ฐ์ฒด๋กœ์„œ ์‚ฌ์šฉํ•˜๋ ค๋ฉด JSON ํฌ๋งท์˜ ๋ฌธ์ž์—ด์„ ๊ฐ์ฒดํ™”ํ•ด์•ผ ํ•˜๋Š”๋ฐ ์ด๋ฅผ ์—ญ์ง๋ ฌํ™”(deserializing)๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค.

 

const obj = {
  name: 'Lee',
  age: 20,
  alive: true,
  hobby: ['traveling', 'tennis']
};

// ๊ฐ์ฒด๋ฅผ JSON ํฌ๋งท์˜ ๋ฌธ์ž์—ด๋กœ ๋ณ€ํ™˜ํ•œ๋‹ค.
const json = JSON.stringify(obj);

// JSON ํฌ๋งท์˜ ๋ฌธ์ž์—ด์„ ๊ฐ์ฒด๋กœ ๋ณ€ํ™˜ํ•œ๋‹ค.
const parsed = JSON.parse(json);
console.log(typeof parsed, parsed);
// object {name: "Lee", age: 20, alive: true, hobby: ["traveling", "tennis"]}

 

๋ฐฐ์—ด์ด JSON ํฌ๋งท์˜ ๋ฌธ์ž์—ด๋กœ ๋ณ€ํ™˜๋˜์–ด ์žˆ๋Š” ๊ฒฝ์šฐ JSON.parse๋Š” ๋ฌธ์ž์—ด์„ ๋ฐฐ์—ด ๊ฐ์ฒด๋กœ ๋ณ€ํ™˜ํ•ฉ๋‹ˆ๋‹ค. ์ด๋•Œ, ๋ฐฐ์—ด์˜ ์š”์†Œ๊ฐ€ ๊ฐ์ฒด์ธ ๊ฒฝ์šฐ ๋ฐฐ์—ด์˜ ์š”์†Œ๊นŒ์ง€ ๊ฐ์ฒด๋ฅผ ๋ณ€ํ™˜ํ•ฉ๋‹ˆ๋‹ค.

 

const todos = [
  { id: 1, content: 'HTML', completed: false },
  { id: 2, content: 'CSS', completed: true },
  { id: 3, content: 'Javascript', completed: false }
];

// ๋ฐฐ์—ด์„ JSON ํฌ๋งท์˜ ๋ฌธ์ž์—ด๋กœ ๋ณ€ํ™˜ํ•œ๋‹ค.
const json = JSON.stringify(todos);

// JSON ํฌ๋งท์˜ ๋ฌธ์ž์—ด์„ ๋ฐฐ์—ด๋กœ ๋ณ€ํ™˜ํ•œ๋‹ค. ๋ฐฐ์—ด์˜ ์š”์†Œ๊นŒ์ง€ ๊ฐ์ฒด๋กœ ๋ณ€ํ™˜๋œ๋‹ค.
const parsed = JSON.parse(json);
console.log(typeof parsed, parsed);
/*
 object [
  { id: 1, content: 'HTML', completed: false },
  { id: 2, content: 'CSS', completed: true },
  { id: 3, content: 'Javascript', completed: false }
]
*/

XMLHttpRequest

๋ธŒ๋ผ์šฐ์ €๋Š” ์ฃผ์†Œ์ฐฝ์ด๋‚˜ HTML์˜ form ํƒœ๊ทธ ๋˜๋Š” a ํƒœ๊ทธ๋ฅผ ํ†ตํ•ด HTTP ์š”์ฒญ ์ „์†ก ๊ธฐ๋Šฅ์„ ๊ธฐ๋ณธ ์ œ๊ณตํ•˜๋Š”๋ฐ, ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ HTTP ์š”์ฒญ์„ ์ „์†กํ•˜๋ ค๋ฉด XMLHttpRequest ๊ฐ์ฒด๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.

 

Web API์ธ XMLHttpRequest ๊ฐ์ฒด๋Š” HTTP ์š”์ฒญ ์ „์†ก๊ณผ HTTP ์‘๋‹ต ์ˆ˜์‹ ์„ ์œ„ํ•œ ๋‹ค์–‘ํ•œ ๋ฉ”์„œ๋“œ์™€ ํ”„๋กœํผํ‹ฐ๋ฅผ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค.

 

XMLHttpRequest ๊ฐ์ฒด ์ƒ์„ฑ

XMLHttpRequest ๊ฐ์ฒด๋Š” XMLHttpRequest ์ƒ์„ฑ์ž ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•˜์—ฌ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค. ์ด ๊ฐ์ฒด๋Š” ๋ธŒ๋ผ์šฐ์ €์—์„œ ์ œ๊ณตํ•˜๋Š” Web API์ด๋ฏ€๋กœ ๋ธŒ๋ผ์šฐ์ € ํ™˜๊ฒฝ์—์„œ๋งŒ ์ •์ƒ์ ์œผ๋กœ ์‹คํ–‰๋ฉ๋‹ˆ๋‹ค.

 

// XMLHttpRequest ๊ฐ์ฒด ์ƒ์„ฑ
const xhr = new XMLHttpRequest();

 

XMLHttpRequest ๊ฐ์ฒด์˜ ํ”„๋กœํผํ‹ฐ์™€ ๋ฉ”์„œ๋“œ

XMLHttpRequest ๊ฐ์ฒด์˜ ๋‹ค์–‘ํ•œ ํ”„๋กœํผํ‹ฐ์™€ ๋ฉ”์„œ๋“œ ์ค‘ ๋Œ€ํ‘œ์ ์ธ ํ”„๋กœํผํ‹ฐ์™€ ๋ฉ”์„œ๋“œ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.

 

ํ”„๋กœํ† ํƒ€์ž… ํ”„๋กœํผํ‹ฐ

ํ”„๋กœํ† ํƒ€์ž… ํ”„๋กœํผํ‹ฐ ์„ค๋ช…
readyState HTTP ์š”์ฒญ์˜ ํ˜„์žฌ ์ƒํƒœ๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” ์ •์ˆ˜

- UNSENT : 0
- OPENED : 1
- HEADERS_RECEIVED : 2
- LOADING : 3
- DONE : 4
status HTTP ์š”์ฒญ์— ๋Œ€ํ•œ ์‘๋‹ต ์ƒํƒœ(*HTTP ์ƒํƒœ ์ฝ”๋“œ)๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” ์ •์ˆ˜ (ex. 200)
statusText HTTP ์š”์ฒญ์— ๋Œ€ํ•œ ์‘๋‹ต ๋ฉ”์‹œ์ง€๋ฅผ ํƒ€๋‚˜๋‚ด๋Š” ๋ฌธ์ž์—ด (ex. "OK")
responseType HTTP ์‘๋‹ต ํƒ€์ž… (ex. document, json, text, blob, arraybuffer)
response HTTP ์š”์ฒญ์— ๋Œ€ํ•œ ์‘๋‹ต ๋ชธ์ฒด. responseType์— ๋”ฐ๋ผ ํƒ€์ž…์ด ๋‹ค๋ฆ„
responseText ์„œ๋ฒ„๊ฐ€ ์ „์†กํ•œ HTTP ์š”์ฒญ์— ๋Œ€ํ•œ ์‘๋‹ต ๋ฌธ์ž์—ด

 

*HTTP ์ƒํƒœ ์ฝ”๋“œ : https://j-su2.tistory.com/17?category=1016208 

 

[๋„คํŠธ์›Œํฌ] HTTP์˜ Method์™€ ์ƒํƒœ์ฝ”๋“œ

HTTP๋ž€? https://j-su2.tistory.com/16 [๋„คํŠธ์›Œํฌ] HTTP์™€ HTTPS HTTP๋ž€? Hyper Text Transfer Protocol์˜ ์•ฝ์ž๋กœ, ์ธํ„ฐ๋„ท์—์„œ ํ…Œ์ดํ„ฐ๋ฅผ ์ฃผ๊ณ ๋ฐ›์„ ์ˆ˜ ์žˆ๋Š” ํ”„๋กœํ† ์ฝœ์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. ์ฆ‰, ์›น ์„œ๋ฒ„์™€ ํด๋ผ์ด์–ธํŠธ ๊ฐ„์˜..

j-su2.tistory.com

 

์ด๋ฒคํŠธ ํ•ธ๋“ค๋Ÿฌ ํ”„๋กœํผํ‹ฐ

์ด๋ฒคํŠธ ํ•ธ๋“ค๋Ÿฌ ํ”„๋กœํผํ‹ฐ ์„ค๋ช…
onreadystatechange readyState ํ”„๋กœํผํ‹ฐ ๊ฐ’์ด ๋ณ€๊ฒฝ๋œ ๊ฒฝ์šฐ
onloadstart HTTP ์š”์ฒญ์— ๋Œ€ํ•œ ์‘๋‹ต์„ ๋ฐ›๊ธฐ ์‹œ์ž‘ํ•œ ๊ฒฝ์šฐ
onprogress HTTP ์š”์ฒญ์— ๋Œ€ํ•œ ์‘๋‹ต์„ ๋ฐ›๋Š” ๋„์ค‘ ์ฃผ๊ธฐ์ ์œผ๋กœ ๋ฐœ์ƒ
onabort abort ๋ฉ”์„œ๋“œ์— ์˜ํ•ด HTTP ์š”์ฒญ์ด ์ค‘๋‹จ๋œ ๊ฒฝ์šฐ
onerror HTTP ์š”์ฒญ์— ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•œ ๊ฒฝ์šฐ
onload HTTP ์š”์ฒญ์ด ์„ฑ๊ณต์ ์œผ๋กœ ์™„๋ฃŒํ•œ ๊ฒฝ์šฐ
ontimeout HTTP ์š”์ฒญ ์‹œ๊ฐ„์ด ์ดˆ๊ณผํ•œ ๊ฒฝ์šฐ
onloadend HTTP ์š”์ฒญ์ด ์™„๋ฃŒํ•œ ๊ฒฝ์šฐ. HTTP ์š”์ฒญ์ด ์„ฑ๊ณต/์‹คํŒจํ•˜๋ฉด ๋ฐœ์ƒ

 

๋ฉ”์„œ๋“œ

๋ฉ”์„œ๋“œ ์„ค๋ช…
open HTTP ์š”์ฒญ ์ดˆ๊ธฐํ™”
send HTTP ์š”์ฒญ ์ „์†ก
abort ์ด๋ฏธ ์ „์†ก๋œ HTTP ์š”์ฒญ ์ค‘๋‹จ
setRequestHeader ํŠน์ • HTTP ์š”์ฒญ ํ—ค๋”์˜ ๊ฐ’์„ ์„ค์ •
getResponseHeader ํŠน์ • HTTP ์š”์ฒญ ํ—ค๋”์˜ ๊ฐ’์„ ๋ฌธ์ž์—ด๋กœ ๋ณ€ํ™˜

 

์ •์  ํ”„๋กœํผํ‹ฐ

์ •์  ํ”„๋กœํผํ‹ฐ ๊ฐ’ ์„ค๋ช…
UNSENT 0 open ๋ฉ”์„œ๋“œ ํ˜ธ์ถœ ์ด์ „
OPENED 1 open ๋ฉ”์„œ๋“œ ํ˜ธ์ถœ ์ดํ›„
HEADERS_RECEIVED 2 send ๋ฉ”์„œ๋“œ ํ˜ธ์ถœ ์ดํ›„
LOADING 3 ์„œ๋ฒ„ ์‘๋‹ต ์ค‘(์‘๋‹ต ๋ฐ์ดํ„ฐ ๋ฏธ์™„์„ฑ ์ƒํƒœ)
DONE 4 ์„œ๋ฒ„ ์‘๋‹ต ์™„๋ฃŒ

 

HTTP ์š”์ฒญ ์ „์†ก

HTTP ์š”์ฒญ์„ ์ „์†กํ•˜๋Š” ๊ฒฝ์šฐ๋Š” ๋‹ค์Œ ์ˆœ์„œ๋ฅผ ๋”ฐ๋ฆ…๋‹ˆ๋‹ค.

  1. XMLHttpRequest.prototype.open ๋ฉ”์„œ๋“œ๋กœ HTTP ์š”์ฒญ์„ ์ดˆ๊ธฐํ™”
  2. ํ•„์š”์— ๋”ฐ๋ผ XMLHttpRequest.prototype.setRequstHeader ๋ฉ”์„œ๋“œ๋กœ ํŠน์ • HTTP ์š”์ฒญ์˜ ํ—ค๋” ๊ฐ’์„ ์„ค์ •
  3. XMLHttpRequest.prototype.send ๋ฉ”์„œ๋“œ๋กœ HTTP ์š”์ฒญ์„ ์ „์†ก
// XMLHttpRequest ๊ฐ์ฒด ์ƒ์„ฑ
const xhr = new XMLHttpRequest();

// HTTP ์š”์ฒญ ์ดˆ๊ธฐํ™”
xhr.open('GET', '/users');

// HTTP ์š”์ฒญ ํ—ค๋” ์„ค์ •
// ํด๋ผ์ด์–ธํŠธ๊ฐ€ ์„œ๋ฒ„๋กœ ์ „์†กํ•  ๋ฐ์ดํ„ฐ์˜ MIME ํƒ€์ž… ์ง€์ •: json
xhr.setRequestHeader('content-type', 'application/json');

// HTTP ์š”์ฒญ ์ „์†ก
xhr.send();

 

XMLHttpRequest.prototype.open

open ๋ฉ”์„œ๋“œ๋Š” ์„œ๋ฒ„์— ์ „์†กํ•  HTTP ์š”์ฒญ์„ ์ดˆ๊ธฐํ™”ํ•ฉ๋‹ˆ๋‹ค.

 

xhr.open(method, url[, async]);
๋งค๊ฐœ๋ณ€์ˆ˜ ์„ค๋ช…
method HTTP ์š”์ฒญ ๋ฉ”์„œ๋“œ("GET", "POST", "PUT", "DELETE" ๋“ฑ)
url HTTP ์š”์ฒญ์„ ์ „์†กํ•  URL
async ๋น„๋™๊ธฐ ์š”์ฒญ ์—ฌ๋ถ€. ์˜ต์…˜์œผ๋กœ ๊ธฐ๋ณธ๊ฐ’์€ true์ด๋ฉฐ, ๋น„๋™๊ธฐ ๋ฐฉ์‹์œผ๋กœ ๋™์ž‘

 

HTTP ์š”์ฒญ ๋ฉ”์„œ๋“œ๋Š” ํด๋ผ์ด์–ธํŠธ๊ฐ€ ์„œ๋ฒ„์—์„œ ์š”์ฒญ์˜ ์ข…๋ฅ˜์™€ ๋ชฉ์ (๋ฆฌ์†Œ์Šค์— ๋Œ€ํ•œ ํ–‰์œ„)์„ ์•Œ๋ฆฌ๋Š” ๋ฐฉ๋ฒ•์ž…๋‹ˆ๋‹ค.

์•„๋ž˜์™€ ๊ฐ™์ด ์ฃผ๋กœ 5๊ฐ€์ง€ ์š”์ฒญ ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ CRUD๋ฅผ ๊ตฌํ˜„ํ•ฉ๋‹ˆ๋‹ค.

 

HTTP ์š”์ฒญ ๋ฉ”์†Œ๋“œ ์ข…๋ฅ˜ ๋ชฉ์  ํŽ˜์ด๋กœ๋“œ
GET index/retrieve ๋ชจ๋“ /ํŠน์ • ๋ฆฌ์†Œ๋“œ ์ทจ๋“ X
POST create ๋ฆฌ์†Œ์Šค ์ƒ์„ฑ O
PUT replace ๋ฆฌ์†Œ์Šค์˜ ์ „์ฒด ๊ต์ฒด O
PATCH modify ๋ฆฌ์†Œ์Šค์˜ ์ผ๋ถ€ ์ˆ˜์ • O
DELETE delete ๋ชจ๋“ /ํŠน์ • ๋ฆฌ์†Œ์Šค ์‚ญ์ œ X

 

XMLHttpRequest.prototype.send

send ๋ฉ”์„œ๋“œ๋Š” open ๋ฉ”์„œ๋“œ๋กœ ์ดˆ๊ธฐํ™”๋œ HTTP ์š”์ฒญ์„ ์„œ๋ฒ„์— ์ „์†กํ•ฉ๋‹ˆ๋‹ค. ๊ธฐ๋ณธ์ ์œผ๋กœ ์„œ๋ฒ„๋กœ ์ „์†กํ•˜๋Š” ๋ฐ์ดํ„ฐ๋Š” GET, POST ์š”์ฒญ ๋ฉ”์„œ๋“œ์— ๋”ฐ๋ผ ์ „์†ก ๋ฐฉ์‹์— ์ฐจ์ด๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค.

 

GET ์š”์ฒญ ๋ฉ”์„œ๋“œ

  • ๋ฐ์ดํ„ฐ๋ฅผ URL์˜ ์ผ๋ถ€๋ถ„์ธ ์ฟผ๋ฆฌ ๋ฌธ์ž์—ด(query string)๋กœ ์„œ๋ฒ„์— ์ „์†ก
  • ๋ฐ์ดํ„ฐ(ํŽ˜์ด๋กœ๋“œ, payload)๋กœ ์ „๋‹ฌํ•œ ์ธ์ˆ˜๋Š” ๋ฌด์‹œ๋˜๊ณ  ์š”์ฒญ ๋ชธ์ฒด(request body)๋Š” null๋กœ ์„ค์ •

POST ์š”์ฒญ ๋ฉ”์„œ๋“œ

  • ๋ฐ์ดํ„ฐ(ํŽ˜์ด๋กœ๋“œ, payload)๋ฅผ ์š”์ฒญ ๋ชธ์ฒด(request body)์— ๋‹ด์•„ ์ „์†ก
  • request body์— ๋‹ด์•„ ์ „์†กํ•  ๋ฐ์ดํ„ฐ๋ฅผ ์ธ์ˆ˜๋กœ ์ „๋‹ฌํ•  ์ˆ˜ ์žˆ์Œ
  • ํŽ˜์ด๋กœ๋“œ๊ฐ€ ๊ฐ์ฒด์ธ ๊ฒฝ์šฐ ๋ฐ˜๋“œ์‹œ JSON.stringigy ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ง๋ ฌํ™”ํ•œ ๋‹ค์Œ ์ „๋‹ฌํ•ด์•ผ ํ•จ
xhr.send(JSON.stringify({ id: 1, content: 'HTML', completed: false }));

 

XMLHttpRequest.prototype.setRequstHeader

ํŠน์ • HTTP ์š”์ฒญ์˜ ํ—ค๋” ๊ฐ’์„ ์„ค์ •ํ•˜๋Š” setRequstHeader ๋ฉ”์„œ๋“œ๋Š” ๋ฐ˜๋“œ์‹œ open ๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœํ•œ ์ดํ›„์— ํ˜ธ์ถœํ•ด์•ผํ•ฉ๋‹ˆ๋‹ค.

setRequstHeader ๋ฉ”์„œ๋“œ๋กœ ์ฃผ๋กœ HTTP ์š”์ฒญ ํ—ค๋”์ธ Content-type๊ณผ Accept๋ฅผ ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค.

 

Content-type์€ ์š”์ฒญ ๋ชธ์ฒด์— ๋‹ด์•„ ์ „์†กํ•  ๋ฐ์ดํ„ฐ์˜ MIME ํƒ€์ž…์˜ ์ •๋ณด๋ฅผ ํ‘œํ˜„ํ•ฉ๋‹ˆ๋‹ค.

 

MIME ํƒ€์ž… ์„œ๋ธŒ ํƒ€์ž…
text text/plain, text/html, text/css, text/javascipt
application application/json, application/x-www-form-urlencode
multipart multipart/formed-data
// XMLHttpRequest ๊ฐ์ฒด ์ƒ์„ฑ
const xhr = new XMLHttpRequest();

// HTTP ์š”์ฒญ ์ดˆ๊ธฐํ™”
xhr.open('POST', '/users');

// HTTP ์š”์ฒญ ํ—ค๋” ์„ค์ •
// ํด๋ผ์ด์–ธํŠธ๊ฐ€ ์„œ๋ฒ„๋กœ ์ „์†กํ•  ๋ฐ์ดํ„ฐ์˜ MIME ํƒ€์ž… ์ง€์ •: json
xhr.setRequestHeader('content-type', 'application/json');

// HTTP ์š”์ฒญ ์ „์†ก
xhr.send(JSON.stringify({ id: 1, content: 'HTML', completed: false }));

 

HTTP ํด๋ผ์ด์–ธํŠธ๊ฐ€ ์„œ๋ฒ„์— ์š”์ฒญํ•  ๋•Œ ์„œ๋ฒ„๊ฐ€ ์‘๋‹ตํ•  ๋ฐ์ดํ„ฐ์˜ MIME ํƒ€์ž…์„ Accept๋กœ ์ง€์ •ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋งŒ์•ฝ Accept ํ—ค๋”๋ฅผ ์„ค์ •ํ•˜์ง€ ์•Š์œผ๋ฉด send ๋ฉ”์„œ๋“œ๊ฐ€ ํ˜ธ์ถœ๋  ๋•Œ Accept ํ—ค๋”๊ฐ€ */*์œผ๋กœ ์ „์†ก๋ฉ๋‹ˆ๋‹ค.

 

// ์„œ๋ฒ„๊ฐ€ ์‘๋‹ตํ•  ๋ฐ์ดํ„ฐ์˜ MIME ํƒ€์ž… ์ง€์ •: json
xhr.setRequestHeader('accept', 'application/json');

 

HTTP ์‘๋‹ต ์ฒ˜๋ฆฌ

์„œ๋ฒ„๊ฐ€ ์ „์†กํ•œ ์‘๋‹ต์„ ์ฒ˜๋ฆฌํ•˜๋ ค๋ฉด XMLHttpRequest ๊ฐ์ฒด๊ฐ€ ๋ฐœ์ƒ์‹œํ‚ค๋Š” ์ด๋ฒคํŠธ๋ฅผ ์บ์น˜ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

 

XMLHttpRequest ๊ฐ์ฒด์˜ ์ด๋ฒคํŠธ ํ•ธ๋“ค๋Ÿฌ ํ”„๋กœํผํ‹ฐ ์ค‘์—์„œ HTTP ์š”์ฒญ์˜ ํ˜„์žฌ ์ƒํƒœ๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” readyState ํ”„๋กœํผํ‹ฐ ๊ฐ’์ด ๋ณ€๊ฒฝ๋œ ๊ฒฝ์šฐ ๋ฐœ์ƒํ•˜๋Š” readystatechange ์ด๋ฒคํŠธ๋ฅผ ์บ์น˜ํ•˜์—ฌ ๋‹ค์Œ๊ณผ ๊ฐ™์ด HTTP ์‘๋‹ต์„ ์ฒ˜๋ฆฌํ•ฉ๋‹ˆ๋‹ค.

 

// XMLHttpRequest ๊ฐ์ฒด ์ƒ์„ฑ
const xhr = new XMLHttpRequest();

// HTTP ์š”์ฒญ ์ดˆ๊ธฐํ™”
// https://jsonplaceholder.typicode.com์€ Fake REST API๋ฅผ ์ œ๊ณตํ•˜๋Š” ์„œ๋น„์Šค๋‹ค.
xhr.open('GET', 'https://jsonplaceholder.typicode.com/todos/1');

// HTTP ์š”์ฒญ ์ „์†ก
xhr.send();

// readystatechange ์ด๋ฒคํŠธ๋Š” HTTP ์š”์ฒญ์˜ ํ˜„์žฌ ์ƒํƒœ๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” readyState ํ”„๋กœํผํ‹ฐ๊ฐ€ ๋ณ€๊ฒฝ๋  ๋•Œ๋งˆ๋‹ค ๋ฐœ์ƒ
xhr.onreadystatechange = () => {
  // readyState ํ”„๋กœํผํ‹ฐ๋Š” HTTP ์š”์ฒญ์˜ ํ˜„์žฌ ์ƒํƒœ๋ฅผ ๋‚˜ํƒ€๋‚ธ๋‹ค.
  // readyState ํ”„๋กœํผํ‹ฐ ๊ฐ’์ด 4(XMLHttpRequest.DONE)๊ฐ€ ์•„๋‹ˆ๋ฉด ์„œ๋ฒ„ ์‘๋‹ต์ด ์™„๋ฃŒ๋˜์ง€ ์ƒํƒœ๋‹ค.
  // ๋งŒ์•ฝ ์„œ๋ฒ„ ์‘๋‹ต์ด ์•„์ง ์™„๋ฃŒ๋˜์ง€ ์•Š์•˜๋‹ค๋ฉด ์•„๋ฌด๋Ÿฐ ์ฒ˜๋ฆฌ๋ฅผ ํ•˜์ง€ ์•Š๋Š”๋‹ค.
  if (xhr.readyState !== XMLHttpRequest.DONE) return;

  // status ํ”„๋กœํผํ‹ฐ๋Š” ์‘๋‹ต ์ƒํƒœ ์ฝ”๋“œ๋ฅผ ๋‚˜ํƒ€๋‚ธ๋‹ค.
  // status ํ”„๋กœํผํ‹ฐ ๊ฐ’์ด 200์ด๋ฉด ์ •์ƒ์ ์œผ๋กœ ์‘๋‹ต๋œ ์ƒํƒœ์ด๊ณ 
  // status ํ”„๋กœํผํ‹ฐ ๊ฐ’์ด 200์ด ์•„๋‹ˆ๋ฉด ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•œ ์ƒํƒœ๋‹ค.
  // ์ •์ƒ์ ์œผ๋กœ ์‘๋‹ต๋œ ์ƒํƒœ๋ผ๋ฉด response ํ”„๋กœํผํ‹ฐ์— ์„œ๋ฒ„์˜ ์‘๋‹ต ๊ฒฐ๊ณผ๊ฐ€ ๋‹ด๊ฒจ ์žˆ๋‹ค.
  if (xhr.status === 200) {
    console.log(JSON.parse(xhr.response));
    // {userId: 1, id: 1, title: "delectus aut autem", completed: false}
  } else {
    console.error('Error', xhr.status, xhr.statusText);
  }
};
  1. ์–ธ์ œ ์‘๋‹ต์ด ํด๋ผ์ด์–ธํŠธ์— ๋„๋‹ฌํ• ์ง€๋Š” ์•Œ ์ˆ˜ ์—†๊ธฐ ๋•Œ๋ฌธ์— HTTP ์š”์ฒญ์˜ ํ˜„์žฌ ์ƒํƒœ๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” readyState ํ”„๋กœํผํ‹ฐ๊ฐ€ ๋ณ€๊ฒฝ๋  ๋•Œ๋งˆ๋‹ค DONE์ธ์ง€ ํ™•์ธํ•˜์—ฌ ์„œ๋ฒ„์˜ ์‘๋‹ต์ด ์™„๋ฃŒ๋˜์—ˆ๋Š”์ง€ ํ™•์ธ
  2. ์„œ๋ฒ„์˜ ์‘๋‹ต์ด ์™„๋ฃŒ๋˜๋ฉด HTTP ์ƒํƒœ ์ฝ”๋“œ๊ฐ€ 200์ธ์ง€ ํ™•์ธํ•˜์—ฌ ์ •์ƒ ์ฒ˜๋ฆฌ์™€ ์—๋Ÿฌ ์ฒ˜๋ฆฌ๋ฅผ ๊ตฌ๋ถ„
  3. ์‘๋‹ต์ด ์ •์ƒ์ ์œผ๋กœ ๋„์ฐฉํ–ˆ๋‹ค๋ฉด ์š”์ฒญ์— ๋Œ€ํ•œ ์‘๋‹ต ๋ชธ์ฒด๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” xhr.responce์—์„œ ์„œ๋ฒ„๊ฐ€ ์ „์†กํ•œ ๋ฐ์ดํ„ฐ๋ฅผ ์ทจ๋“
  4. ๋งŒ์•ฝ xhr.status๊ฐ€ 200์ด ์•„๋‹ˆ๋ฉด ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•œ ์ƒํƒœ์ด๋ฏ€๋กœ ํ•„์š”ํ•œ ์—๋Ÿฌ ์ฒ˜๋ฆฌ๋ฅผ ํ•จ

์ด๋Ÿฌํ•œ readystatechange ์ด๋ฒคํŠธ ๋Œ€์‹  HTTP ์š”์ฒญ์ด ์„ฑ๊ณต์ ์œผ๋กœ ์™„๋ฃŒ๋œ ๊ฒฝ์šฐ ๋ฐœ์ƒํ•˜๋Š” load ์ด๋ฒคํŠธ๋ฅผ ์บ์น˜ํ•ด๋„ ๋ฉ๋‹ˆ๋‹ค.

 

// XMLHttpRequest ๊ฐ์ฒด ์ƒ์„ฑ
const xhr = new XMLHttpRequest();

// HTTP ์š”์ฒญ ์ดˆ๊ธฐํ™”
// https://jsonplaceholder.typicode.com์€ Fake REST API๋ฅผ ์ œ๊ณตํ•˜๋Š” ์„œ๋น„์Šค๋‹ค.
xhr.open('GET', 'https://jsonplaceholder.typicode.com/todos/1');

// HTTP ์š”์ฒญ ์ „์†ก
xhr.send();

// load ์ด๋ฒคํŠธ๋Š” HTTP ์š”์ฒญ์ด ์„ฑ๊ณต์ ์œผ๋กœ ์™„๋ฃŒ๋œ ๊ฒฝ์šฐ ๋ฐœ์ƒํ•œ๋‹ค.
xhr.onload = () => {
  // status ํ”„๋กœํผํ‹ฐ๋Š” ์‘๋‹ต ์ƒํƒœ ์ฝ”๋“œ๋ฅผ ๋‚˜ํƒ€๋‚ธ๋‹ค.
  // status ํ”„๋กœํผํ‹ฐ ๊ฐ’์ด 200์ด๋ฉด ์ •์ƒ์ ์œผ๋กœ ์‘๋‹ต๋œ ์ƒํƒœ์ด๊ณ 
  // status ํ”„๋กœํผํ‹ฐ ๊ฐ’์ด 200์ด ์•„๋‹ˆ๋ฉด ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•œ ์ƒํƒœ๋‹ค.
  // ์ •์ƒ์ ์œผ๋กœ ์‘๋‹ต๋œ ์ƒํƒœ๋ผ๋ฉด response ํ”„๋กœํผํ‹ฐ์— ์„œ๋ฒ„์˜ ์‘๋‹ต ๊ฒฐ๊ณผ๊ฐ€ ๋‹ด๊ฒจ ์žˆ๋‹ค.
  if (xhr.status === 200) {
    console.log(JSON.parse(xhr.response));
    // {userId: 1, id: 1, title: "delectus aut autem", completed: false}
  } else {
    console.error('Error', xhr.status, xhr.statusText);
  }
};

[์ถœ์ฒ˜] ๋ชจ๋˜ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ Deep Dive

https://wikibook.co.kr/mjs/

 

๋ชจ๋˜ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ Deep Dive: ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์˜ ๊ธฐ๋ณธ ๊ฐœ๋…๊ณผ ๋™์ž‘ ์›๋ฆฌ

269๊ฐœ์˜ ๊ทธ๋ฆผ๊ณผ ์›๋ฆฌ๋ฅผ ํŒŒํ—ค์น˜๋Š” ์„ค๋ช…์œผ๋กœ ‘์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์˜ ๊ธฐ๋ณธ ๊ฐœ๋…๊ณผ ๋™์ž‘ ์›๋ฆฌ’๋ฅผ ์ดํ•ดํ•˜์ž! ์›นํŽ˜์ด์ง€์˜ ๋‹จ์ˆœํ•œ ๋ณด์กฐ ๊ธฐ๋Šฅ์„ ์ฒ˜๋ฆฌํ•˜๊ธฐ ์œ„ํ•œ ์ œํ•œ์ ์ธ ์šฉ๋„๋กœ ํƒœ์–ด๋‚œ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ๋Š” ๊ณผ๋„

wikibook.co.kr

'JavaScript > ๋ชจ๋˜ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ Deep Dive' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

[JavaScript] ํ”„๋กœ๋ฏธ์Šค(Promise)  (0) 2022.08.17
[JavaScript] REST API  (0) 2022.08.16
[JavaScript] ํƒ€์ด๋จธ  (0) 2022.08.13
[JavaScript] ์ด๋ฒคํŠธ  (0) 2022.08.13
[JavaScript] DOM  (0) 2022.08.12