๐ AJAX๋?
์๋ฐ์คํฌ๋ฆฝํธ๋ฅผ ์ฌ์ฉํ ๋น๋๊ธฐ ํต์ ์ด๋ฉฐ, ํด๋ผ์ด์ธํธ์ ์๋ฒ๊ฐ์ XML ๋ฐ์ดํฐ๋ฅผ ์ฃผ๊ณ ๋ฐ๋ ๊ธฐ์ ์ด๋ค.
XMLHttpRequest ๊ฐ์ฒด๋ฅผ ์ด์ฉํด์ ์ ์ฒด ํ์ด์ง๋ฅผ ๋ฆฌ๋ก๋ํ์ง ์๊ณ ํ์ํ ๋ฐ์ดํฐ๋ง ๋ก๋ํ ์ ์๋ค๋ ์ฅ์ ์ด ์๋ค.
๐ XMLHttpRequest
AJAX๋ ๋ค์ํ ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ฅผ ๊ฐ์ง๊ณ ์๋๋ฐ, ์ด ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ค์ ๋ชจ๋ XMLHttpRequest๋ฅผ ๊ธฐ๋ฐ์ผ๋ก ํ๋ค.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
|
// GET ์์ฒญ
var xhr = new XMLHttpRequest();
xhr.onload = function() {
if (xhr.status === 200 || xhr.status === 201) {
console.log(xhr.responseText);
} else {
console.error(xhr.responseText);
}
};
xhr.open('GET', 'https://www.zerocho.com/api/get?name=zerocho');
xhr.send();
// POST ์์ฒญ
var xhr = new XMLHttpRequest();
var data = {
property_one: value_one,
property_two: value_two
};
xhr.onload = function() {
if (xhr.status === 200 || xhr.status === 201) {
console.log(xhr.responseText);
} else {
console.error(xhr.responseText);
}
};
xhr.open('POST', 'https://someurl.com');
xhr.setRequestHeader('Content-Type', 'application/json'); // ์ปจํ
์ธ ํ์
์ json์ผ๋ก
xhr.send(JSON.stringify(data)); // ๋ฐ์ดํฐ๋ฅผ stringifyํด์ ๋ณด๋
|
cs |
๐ AJAX ๋ผ์ด๋ธ๋ฌ๋ฆฌ
โพ axios
- ๋๋ถ๋ถ์ ๋ธ๋ผ์ฐ์ ๋ฅผ ์ง์ํ๋ค.
- ๋ฐ๋ก import(์ค์น)๊ฐ ํ์ํ๋ค.
- HTTP ์์ฒญ ์ทจ์๊ฐ ๊ฐ๋ฅํ๋ค.
- HTTP ์์ฒญ๊ณผ ์๋ต์ JSON ํํ๋ก ์๋ ๋ณ๊ฒฝํ๋ค. (.json() ์ฌ์ฉ ํ์ X)
- ์๋ต์๊ฐ ์ด๊ณผ๋ฅผ ์ค์ ํ์ฌ, ๋คํธ์ํฌ ์๋ฌ๊ฐ ๋ฐ์ํ์ ๋ ์ค๋ ๊ธฐ๋ค๋ฆฌ์ง ์์ ์ ์๋ค. (response timeout API ์ ๊ณต O)
- catch์ ๊ฑธ๋ ธ์ ๋, .then์ ์คํํ์ง ์๊ณ .console ์ฐฝ์ ํด๋น ์๋ฌ ๋ก๊ทธ๋ฅผ ๋ณด์ฌ์ค๋ค.
- return ๊ฐ์ Promise ๊ฐ์ฒด ํํ์ด๋ค.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
let url = 'https://someurl.com';
let options = {
method: 'POST',
url: url,
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json;charset=UTF-8'
},
data: {
property_one: value_one,
property_two: value_two
}
};
let response = await axios(options);
let responseOK = response && response.status === 200 && response.statusText === 'OK';
if (responseOK) {
let data = await response.data;
}
|
cs |
โพ fecth
- ์ง์ํ์ง ์๋ ๋ธ๋ผ์ฐ์ ๊ฐ ์๋ค.
- ์๋ฐ์คํฌ๋ฆฝํธ ๋ด์ฅ ๋ผ์ด๋ธ๋ฌ๋ฆฌ์ด๊ธฐ ๋๋ฌธ์ import ํ์ง ์๊ณ ์ฌ์ฉํ ์ ์๋ค.
- ๋ผ์ด๋ธ๋ฌ๋ฆฌ์ ์ ๋ฐ์ดํธ์ ๋ฐ๋ฅธ ์๋ฌ ๋ฐฉ์ง๊ฐ ๊ฐ๋ฅํ๋ค. (React Native์ ๊ฒฝ์ฐ ์ ๋ฐ์ดํธ๊ฐ ์ฆ์์ ๋ผ์ด๋ธ๋ฌ๋ฆฌ๊ฐ ์ซ์์ค์ง ๋ชปํ๋ ๊ฒฝ์ฐ๊ฐ ๋ง์๋ฐ, fetch์ ๊ฒฝ์ฐ ์ด๋ฅผ ๋ฐฉ์งํ ์ ์๋ค.)
- HTTP ์์ฒญ ์ทจ์๊ฐ ๋ถ๊ฐ๋ฅํ๋ค.
- HTTP ์์ฒญ๊ณผ ์๋ต์ JSON ํํ๋ก ์๋ ๋ณ๊ฒฝํ์ง ์๋๋ค. (.json() ์ฌ์ฉ ํ์ O)
- ๋คํธ์ํฌ ์๋ฌ๊ฐ ๋ฐ์ํ์ ๋ ๊ธฐ๋ค๋ ค์ผ ํ๋ค. (response timeout API ์ ๊ณต X)
- return ๊ฐ์ Promise ๊ฐ์ฒด ํํ์ด๋ค.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
let url = 'https://someurl.com';
let options = {
method: 'POST',
mode: 'cors',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json;charset=UTF-8'
},
body: JSON.stringify({
property_one: value_one,
property_two: value_two
})
};
let response = await fetch(url, options);
let responseOK = response && response.ok;
if (responseOK) {
let data = await response.json();
}
|
cs |
โ fetch์ request ํจ์๋ response ๊ฐ์ฒด๊ฐ ok ํ๋กํผํฐ๋ฅผ ํฌํจํ๋ ๊ฒ์ผ๋ก ์ ์์ ์ธ ์์ฒญ/์๋ต์ ์ฒดํฌํ๊ณ , axios๋ status ๊ฐ์ด 200์ธ์ง์ statusText๋ฅผ ํตํด์ ํ์ธํ๋ค.
โ fetch๋ response ๊ฐ์ฒด์ .json() ๋ฉ์๋๋ฅผ ํธ์ถํ์ฌ json ๊ฐ์ฒด๋ฅผ ์ป๊ณ , axios๋ response ๊ฐ์ฒด์ data property์ ์ ๊ทผํ์ฌ ์ป๋๋ค.
๐ ์ฐธ๊ณ ์๋ฃ
๐ก ๊ฐ์ด ๋ณด๋ฉด ์ข์ Post
'๐ > ใด JavaScript' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[JavaScript] ํ๋กํ ํ์ (prototype) ์ด๋ (0) | 2021.03.08 |
---|---|
[JavaScript] Axios ์ฌ์ฉํ์ฌ ์น์๋ฒ์ ํต์ ํ๊ธฐ (0) | 2021.03.07 |
[JavaScript] this๋? / call, apply, bind ์ฐจ์ด์ (0) | 2021.02.01 |
[JavaScript] ํด๋ก์ (Closure)๋? (0) | 2021.02.01 |
[JavaScript] ํธ์ด์คํ (Hoisting) ์ด๋? (0) | 2021.02.01 |