-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1286 from narinn-star/narinn-star
43μ₯ Ajax - μ΄λλ¦°
- Loading branch information
Showing
1 changed file
with
256 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,256 @@ | ||
# π― 43 Ajax | ||
|
||
[43.1 Ajaxλ?](#1-ajaxλ) | ||
[43.2 JSON](#2-json) | ||
[43.3 XMLHttpRequest](#3-xmlhttprequest) | ||
|
||
## 1. Ajaxλ? | ||
|
||
μλ°μ€ν¬λ¦½νΈλ₯Ό μ¬μ©νμ¬ λΈλΌμ°μ κ° μλ²μκ² λΉλκΈ° λ°©μμΌλ‘ λ°μ΄ν°λ₯Ό μμ²νκ³ , μλ²κ° μλ΅ν λ°μ΄ν°λ₯Ό μμ ν΄ μΉνμ΄μ§λ₯Ό λμ μΌλ‘ κ°±μ νλ νλ‘κ·Έλλ° λ°©μ | ||
|
||
Ajaxλ λΈλΌμ°μ μμ μ 곡νλ Web APIμΈ XMLHttpRequest κ°μ²΄λ₯Ό κΈ°λ°μΌλ‘ λμνλ€. XMLHttpRequestλ HTTP λΉλκΈ° ν΅μ μ μν λ©μλμ νλ‘νΌν°λ₯Ό μ 곡νλ€. | ||
|
||
μλ²λ‘λΆν° μΉνμ΄μ§μ λ³κ²½μ νμν λ°μ΄ν°λ§ λΉλκΈ° λ°©μμΌλ‘ μ μ‘λ°μ μΉνμ΄μ§λ₯Ό λ³κ²½ν νμκ° μλ λΆλΆμ λ€μ λ λλ§νμ§ μκ³ , λ³κ²½ν νμκ° μλ λΆλΆλ§ λ λλ§νλ λ°©μμ΄ κ°λ₯ν΄μ‘λ€. μ΄λ₯Ό ν΅ν΄ λΈλΌμ°μ μμλ λ°μ€ν¬ν± μ ν리μΌμ΄μ κ³Ό μ μ¬ν λΉ λ₯Έ νΌν¬λ¨Όμ€μ λΆλλ¬μ΄ νλ©΄ μ νμ΄ κ°λ₯ν΄μ‘λ€. | ||
|
||
β¨ Ajaxμ μ₯μ (μ ν΅ λ°©μκ³Ό λΉκ΅) | ||
|
||
- λ³κ²½ν λΆλΆμ κ°±μ νλ λ° νμν λ°μ΄ν°λ§ μλ²λ‘λΆν° μ μ‘λ°κΈ° λλ¬Έμ λΆνμν λ°μ΄ν° ν΅μ μ΄ λ°μνμ§ μλλ€. | ||
- λ³κ²½ν νμκ° μλ λΆλΆμ λ€μ λ λλ§νμ§ μλλ€. λ°λΌμ νλ©΄μ΄ μκ°μ μΌλ‘ κΉλ°μ΄λ νμμ΄ λ°μνμ§ μλλ€. | ||
- ν΄λΌμ΄μΈνΈμ μλ²μμ ν΅μ μ΄ λΉλκΈ° λ°©μμΌλ‘ λμνκΈ° λλ¬Έμ μλ²μκ² μμ²μ λ³΄λΈ μ΄ν λΈλ‘νΉμ΄ λ°μνμ§ μλλ€. | ||
|
||
## 2. JSON | ||
|
||
ν΄λΌμ΄μΈνΈμ μλ² κ°μ HTTP ν΅μ μ μν ν μ€νΈ λ°μ΄ν° ν¬λ§·μΌλ‘, μλ°μ€ν¬λ¦½νΈμ μ’ μλμ§ μλ μΈμ΄ λ 립ν λ°μ΄ν° ν¬λ§·μΌλ‘, λλΆλΆμ νλ‘κ·Έλλ° μΈμ΄μμ μ¬μ©ν μ μλ€. | ||
|
||
## 2.1 JSON νκΈ° λ°©μ | ||
|
||
μλ°μ€ν¬λ¦½νΈμ κ°μ²΄ 리ν°λ΄κ³Ό μ μ¬νκ² ν€μ κ°μΌλ‘ ꡬμ±λ μμν ν μ€νΈλ€. | ||
|
||
ν€λ λ°λμ ν°λ°μ΄νλ‘ λ¬Άμ΄μΌ νλ©°, κ°μ κ°μ²΄ 리ν°λ΄κ³Ό κ°μ νκΈ°λ²μ κ·Έλλ‘ μ¬μ©ν μ μλ€. | ||
λ¬Έμμ΄μ λ°λμ ν°λ°μ΄νλ‘ λ¬Άμ΄μΌ νλ€. | ||
|
||
```json | ||
{ | ||
"name": "Lee", | ||
"age": 20, | ||
"alive": true, | ||
"hobby": ["traveling", "tennis"] | ||
} | ||
``` | ||
|
||
### 2.2 JSON.stringify | ||
|
||
κ°μ²΄λ₯Ό JSON ν¬λ§·μ λ¬Έμμ΄λ‘ λ³ννλ λ©μλ | ||
ν΄λΌμ΄μΈνΈκ° μλ²λ‘ κ°μ²΄λ₯Ό μ μ‘νλ €λ©΄ κ°μ²΄λ₯Ό λ¬Έμμ΄νν΄μΌ νλλ° μ΄λ₯Ό *μ§λ ¬ν*λΌ νλ€. | ||
|
||
```javascript | ||
const obj = { | ||
name: 'Lee', | ||
age: 20, | ||
alive: true, | ||
hobby: ['traveling', 'tennis'], | ||
}; | ||
|
||
const json = JSON.stringify(obj); | ||
console.log(typeof json, json); | ||
// string {"name":"Lee","age":20,"alive":true,"hobby":["traveling","tennis"]} | ||
|
||
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" | ||
] | ||
} | ||
*/ | ||
``` | ||
|
||
κ°μ²΄λΏλ§ μλλΌ λ°°μ΄λ JSON ν¬λ§·μ λ¬Έμμ΄λ‘ λ³νν μ μλ€. | ||
|
||
### 2.3 JSON.parse | ||
|
||
JSON ν¬λ§·μ λ¬Έμμ΄μ κ°μ²΄λ‘ λ³ννλ λ©μλλ€. | ||
μλ²λ‘λΆν° ν΄λΌμ΄μΈνΈμκ² μ μ‘λ JSON λ°μ΄ν°μΈ λ¬Έμμ΄μ κ°μ²΄λ‘μ μ¬μ©νλ €λ©΄ JSON ν¬λ§·μ λ¬Έμμ΄μ κ°μ²΄νν΄μΌ νλλ° μ΄λ₯Ό *μμ§λ ¬ν*λΌ νλ€. | ||
|
||
```javascript | ||
const obj = { | ||
name: 'Lee', | ||
age: 20, | ||
alive: true, | ||
hobby: ['traveling', 'tennis'], | ||
}; | ||
|
||
const json = JSON.stringify(obj); | ||
|
||
const parsed = JSON.parse(json); | ||
console.log(typeof parsed, parsed); | ||
// object {"name":"Lee","age":20,"alive":true,"hobby":["traveling","tennis"]} | ||
``` | ||
|
||
λ°°μ΄μ΄ JSON ν¬λ§·μ λ¬Έμμ΄λ‘ λ³νλμ΄ μλ κ²½μ° JSON.parseλ λ¬Έμμ΄μ λ°°μ΄ κ°μ²΄λ‘ λ³ννλ€. λ°°μ΄μ μμκ° κ°μ²΄μΈ κ²½μ° λ°°μ΄μ μμκΉμ§ κ°μ²΄λ‘ λ³ννλ€. | ||
|
||
## 3. XMLHttpRequest | ||
|
||
μλ°μ€ν¬λ¦½νΈλ₯Ό μ¬μ©ν΄ HTTP μμ²μ μ μ‘νλ €λ©΄ XMLHttpRequest κ°μ²΄λ₯Ό μ¬μ©νλ€. | ||
Web APIμΈ XMLHttpRequest κ°μ²΄λ HTTP μμ² μ μ‘κ³Ό HTTP μλ΅ μμ μ μν λ€μν λ©μλμ νλ‘νΌν°λ₯Ό μ 곡νλ€. | ||
|
||
### 3.1 XMLHttpRequest κ°μ²΄ μμ± | ||
|
||
XMLHttpRequest μμ±μ ν¨μλ₯Ό νΈμΆν΄ μμ±νλ€. | ||
λΈλΌμ°μ μμ μ 곡νλ Web APIμ΄λ―λ‘ λΈλΌμ°μ νκ²½μμλ§ μ μμ μΌλ‘ μ€νλλ€. | ||
|
||
```javascript | ||
const xhr = new XMLHttpRequest(); | ||
``` | ||
|
||
### 3.2 XMLHttpRequest κ°μ²΄μ νλ‘νΌν°μ λ©μλ | ||
|
||
λνμ μΈ νλ‘νΌν°μ λ©μλλ λ€μκ³Ό κ°λ€. | ||
|
||
- XMLHttpRequest κ°μ²΄μ νλ‘ν νμ νλ‘νΌν° | ||
|
||
- readyState | ||
- status | ||
- statusText | ||
- responseType | ||
- response | ||
- HTTP μμ²μ λν μλ΅ λͺΈμ²΄ (response body), responseTypeμ λ°λΌ νμ μ΄ λ€λ¦ | ||
- responseText | ||
|
||
- XMLHttpRequest κ°μ²΄μ μ΄λ²€νΈ νΈλ€λ¬ νλ‘νΌν° | ||
|
||
- onreadystatechange | ||
- onloadstart | ||
- onprogress | ||
- onabort | ||
- abort λ©μλμ μν΄ HTTP μμ²μ΄ μ€λ¨λ κ²½μ° | ||
- onerror | ||
- onload | ||
- ontimeout | ||
- onloadend | ||
|
||
- XMLHttpRequest κ°μ²΄μ λ©μλ | ||
|
||
- open | ||
- send | ||
- abort | ||
- setRequestHeader | ||
- νΉμ HTTP μμ² ν€λμ κ°μ μ€μ | ||
- getRequestHeader | ||
- νΉμ HTTP μμ² ν€λμ κ°μ λ¬Έμμ΄λ‘ λ°ν | ||
|
||
- XMLHttpRequest κ°μ²΄μ μ μ νλ‘νΌν° | ||
- UNSENT (0) : open λ©μλ νΈμΆ μ΄μ | ||
- OPENED (1) : open λ©μλ νΈμΆ μ΄ν | ||
- HEADERS_RECEIVED (2) : send λ©μλ νΈμΆ μ΄ν | ||
- LOADING (3) : μλ² μλ΅ μ€(μλ΅ λ°μ΄ν° λ―Έμμ± μν) | ||
- DONE (4) : μλ² μλ΅ μλ£ | ||
|
||
### 3.3 HTTP μμ² μ μ‘ | ||
|
||
1. XMLHttpRequest.prototype.open λ©μλλ‘ HTTP μμ²μ μ΄κΈ°ν | ||
2. νμμ λ°λΌ XMLHttpRequest.prototype.setRequestHeader λ©μλλ‘ νΉμ HTTP μμ²μ ν€λ κ°μ μ€μ | ||
3. XMLHttpRequest.prototype.send λ©μλλ‘ HTTP μμ²μ μ μ‘ | ||
|
||
```javascript | ||
const xhr = new XMLHttpRequest(); | ||
|
||
xhr.open('GET', '/users'); | ||
|
||
xhr.setRequestHeader('content-type', 'application/json'); | ||
|
||
xhr.send(); | ||
``` | ||
|
||
β¨ **XMLHttpRequest.prototype.open** | ||
|
||
open λ©μλλ μλ²μ μ μ‘ν HTTP μμ²μ μ΄κΈ°ννλ€. | ||
νΈμΆμ μλμ κ°μ΄ νλ€. | ||
|
||
```javascript | ||
xhr.open(method, url[, async]) | ||
``` | ||
|
||
- method : HTTP μμ² λ©μλ | ||
- GET, POST, PUT, PATCH, DELETE | ||
- url : HTTP μμ²μ μ μ‘ν URL | ||
- async : λΉλκΈ° μμ² μ¬λΆ. μ΅μ μΌλ‘ κΈ°λ³Έκ°μ true, λΉλκΈ° λ°©μμΌλ‘ λμνλ€. | ||
|
||
β¨ **XMLHttpRequest.prototype.send** | ||
|
||
send λ©μλλ open λ©μλλ‘ μ΄κΈ°νλ HTTP μμ²μ μλ²μ μ μ‘νλ€. | ||
μλ²λ‘ μ μ‘νλ λ°μ΄ν°λ GET, POST μμ² λ©μλμ λ°λΌ μ μ‘ λ°©μμ μ°¨μ΄κ° μλ€. | ||
|
||
- GET μμ² λ©μλμΌ κ²½μ° λ°μ΄ν°λ₯Ό URLμ μΌλΆλΆμΈ 쿼리 λ¬Έμμ΄λ‘ μλ²μ μ μ‘νλ€. (query string) | ||
- POST μμ² λ©μλμΌ κ²½μ° λ°μ΄ν°(νμ΄λ‘λ, payload)λ₯Ό μμ² λͺΈμ²΄μ λ΄μ μ μ‘νλ€. (request body) | ||
|
||
send λ©μλμλ μμ² λͺΈμ²΄μ λ΄μ μ μ‘ν λ°μ΄ν°λ₯Ό μΈμλ‘ μ λ¬ν μ μλ€. νμ΄λ‘λκ° κ°μ²΄μΈ κ²½μ° λ°λμ JSON.stringify λ©μλλ₯Ό μ¬μ©ν΄ μ§λ ¬νν λ€μ μ λ¬ν΄μΌ νλ€. | ||
|
||
```javascript | ||
xhr.send(JSON.stringify({ id: 1, content: 'HTML', completed: false })); | ||
``` | ||
|
||
βοΈ **HTTP μμ² λ©μλκ° GETμΈ κ²½μ° send λ©μλμ νμ΄λ‘λλ‘ μ λ¬ν μΈμλ 무μλκ³ μμ² λͺΈμ²΄λ nullλ‘ μ€μ λλ€.** | ||
|
||
β¨ **XMLHttpRequest.prototype.setRequestHeader** | ||
|
||
setRequestHeader λ©μλλ νΉμ HTTP μμ²μ ν€λ κ°μ μ€μ νλ©° λ°λμ open λ©μλλ₯Ό νΈμΆν μ΄νμ νΈμΆν΄μΌ νλ€. | ||
|
||
μμ£Ό μ¬μ©νλ HTTP μμ² ν€λμΈ Content-typeκ³Ό Acceptμ λν΄ μ΄ν΄λ³΄μ. | ||
|
||
- Content-type | ||
μμ² λͺΈμ²΄μ λ΄μ μ μ‘ν λ°μ΄ν°μ MIME νμ μ μ 보λ₯Ό νννλ€. | ||
- MIME νμ | ||
text : text/plain, text/html, text/css, text/javascript | ||
application : application/json, application/x-www-form-urlencode | ||
multipart : multipart/formed-data | ||
|
||
```javascript | ||
const xhr = new XMLHttpRequest(); | ||
|
||
xhr.open('POST', '/users'); | ||
|
||
xhr.setRequestHeader('content-type', 'application/json'); | ||
|
||
xhr.send(JSON.stringify({ id: 1, content: 'HTML', completed: false })); | ||
``` | ||
|
||
- Accept | ||
HTTP ν΄λΌμ΄μΈνΈκ° μλ²μ μμ²ν λ μλ΅ν λ°μ΄ν°μ MIME νμ μ Acceptλ‘ μ§μ ν μ μλ€. | ||
Accept ν€λλ₯Ό μ€μ νμ§ μμΌλ©΄ send λ©μλκ° νΈμΆλ λ Accept ν€λκ° */*μΌλ‘ μ μ‘λλ€. | ||
|
||
```javascript | ||
xhr.setRequestHeader('accept', 'application/json'); | ||
``` | ||
|
||
### 3.4 HTTP μλ΅ μ²λ¦¬ | ||
|
||
- send λ©μλλ₯Ό ν΅ν΄ HTTP μμ²μ μλ²μ μ μ‘νλ©΄ μλ²λ μλ΅μ λ°ννλ€. | ||
- readystatechange μ΄λ²€νΈλ₯Ό ν΅ν΄ HTTP μμ²μ νμ¬ μνλ₯Ό νμΈνλ€. | ||
- μλ²μ μλ΅μ΄ μλ£λλ©΄ μλ΅ μνλ₯Ό λνλ΄λ statusκ° 200μΈμ§ νμΈνκ³ μ μ μ²λ¦¬μ μλ¬ μ²λ¦¬λ₯Ό ꡬλΆνλ€. | ||
- μ μμ μΌλ‘ λμ°©νλ€λ©΄ μλ²κ° μ μ‘ν λ°μ΄ν°λ₯Ό responseμμ μ·¨λνλ©° μλ¬κ° λ°μνλ€λ©΄ νμν μλ¬ μ²λ¦¬λ₯Ό νλ€. |