์์ ์ฝ๋
1. index.html
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<script src="./main.js" type="text/javascript"></script>
</head>
<body>
<div class="container">
<h4 class="text-center">๊ฒ์ํ - Main</h4>
<div class="input-group mb-3">
<table class="table table-hover">
<thead>
<tr>
<th>#</th>
<th>์ ๋ชฉ</th>
<th>์์ฑ์</th>
<th>์์ฑ์ผ์</th>
<th>์กฐํ์</th>
</tr>
</thead>
<tbody>
<tr>
<td>{{ board.boardId }}</td>
<td>{{ board.title }}</td>
<td>{{ board.userName }}</td>
<td>{{ board.regDt.date | makeDateStr(".") }}</td>
<td>{{ board.readCount }}</td>
</tr>
</tbody>
</table>
<div style="color: red;" class="btn btn-sm btn-primary">
<a href='javascript:void(0);' onclick="test();">test</a>
<a href="javascript:getHello();">๊ธ์ฐ๊ธฐ</a>
<a href="javascript:getHello2();">HelloPOST</a>
</div>
</div>
</body>
</html>
2. main.js
function init() {
console.log("===init===");
}
init();
async function test() {
console.log("===test===");
}
function getData(url) {
return fetch(url).then(function (data) {
return data.json();
});
}
async function getHello() {
const url = "http://localhost:8080/hello";
const result = await getData(url);
console.log(result);
document.write(result.name);
console.log(result.name);
return result;
}
// JSON.stringify() : JSON๊ฐ์ฒด๋ก ์ง๋ ฌํ (js๊ฐ์ฒด => JSON)
// JSON.parse() : Javascript ๊ฐ์ฒด๋ก ์ญ์ง๋ ฌํ (JSON => js๊ฐ์ฒด)
async function getHello2() {
param = {
test: "[test param]",
};
const url = "http://localhost:8080/hello2";
fetch(url, {
method: "POST",
headers: {
"Content-Type": "application/json",
},
// body: JSON.stringify({
// test: "[test param]",
// }),
body: JSON.stringify(param),
})
// ์ฒซ ๋ฒ์งธ then: ์์ง body๋์ฐฉ ์ , promise ๊ฐ์ฒด
// ๋น๋๊ธฐ๋ก ์ฒ๋ฆฌ๋ fetch ํจ์์ ๊ฒฐ๊ณผ ๊ฐ์ด ๋ค์ด์ค๋ฉด then ๋ฉ์๋์ ์ก์
์ ์คํํ๋ค.
// fetchํจ์๋ ์๋ฒ์์ ์ ๋ฌ๋ฐ์ Promise ๊ฐ์ฒด๋ฅผ ๋ฆฌํด, ๋ฆฌํด๋ ๊ฐ์ฒด๋ฅผ then๋ฉ์๋์์ ์ธ์๋ก ๋ฐ๋๋ค. => ํค๋๋ ๋์ฐฉ, ๋ฐ๋๋ ๋์ฐฉ ์
// .json()์ Promise ๊ฐ์ฒด๋ฅผ ๋ฆฌํดํ๋ค.
// Promise ์๋๋ฐฉ์: fetch ํ ๋ ์์ค๋ response๊ฐ์ฒด๋ ์ผ๋จ header๋จผ์ ๋์ฐฉํจ. ๋ชจ๋ header ๋์ฐฉํ์๋ง์ return๋จ
// ์์ง body๊ฐ ์ ์จ ์ํ์ธ Promise๊ฐ์ฒด๋ฅผ ๋ฐ๋ก ๋ฐํํ๋ ๊ฒ์.
// ์ค๊ฐ์ ๊ธฐ๋ค๋ ธ๋ค๊ฐ body๊น์ง ๋ค ์จ Promise๊ฐ์ฒด๋ฅผ ๋ฆฌํดํ๊ณ ์ถ์ง๋ง ๊ทธ๊ฑด ๋ถ๊ฐ๋ฅํจ.
// ๋๋ฌธ์ .then์ผ๋ก chaining(์ฒด์ด๋)ํด์ ์คํํด์ผ ํ๋ค.
.then((result) => {
return result.json();
})
.then((result) => {
console.log(result);
document.write(result.name);
console.log(result.name);
})
.catch((error) => {
console.log(error);
alert("error๊ฐ ๋ฐ์ํ์์ต๋๋ค.");
});
}
HTTP ๋ฉ์๋์ ๋ฐ๋ฅธ API ํธ์ถ
- GET
async function getHello() {
const url = "http://localhost:8080/hello";
const result = await getData(url);
console.log(result.name);
return result;
}
- POST
Promise ๊ฐ์ฒด ์ฒด์ด๋X
/**
* Promise ์ฒด์ด๋X
*/
async function getHello3() {
param = {
test: "[test param]",
};
const url = "http://localhost:8080/hello2";
const res = await fetch(url, {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(param),
});
const result = await res.json();
console.log(result.name);
}
Promise ๊ฐ์ฒด ์ฒด์ด๋O
async function getHello2() {
param = {
test: "[test param]",
};
const url = "http://localhost:8080/hello2";
fetch(url, {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(param),
})
.then((result) => {
return result.json(); //์ฌ๊ธฐ์ Promise ๊ฐ์ฒด๊ฐ ๋์ด
})
.then((result) => {
console.log(result.name);
})
.catch((error) => {
console.log(error);
alert("error๊ฐ ๋ฐ์ํ์์ต๋๋ค.");
});
}
1. then์ 2๋ฒ ์ฐ๋ ์ด์
- Promise์ ์๋ ๋ฐฉ์ ๋๋ฌธ์ด๋ค.
- .json์ ํด๋ json๊ฐ์ฒด๊ฐ ์๋ Promise๊ฐ์ฒด๊ฐ ๋ฆฌํด๋๋ค.
* ๋น๋๊ธฐ๋ก ์ฒ๋ฆฌ๋ fetch()๋ ๊ฒฐ๊ณผ ๊ฐ์ ๋ฐ์ผ๋ฉด then๋ฉ์๋์ ์ก์ ์ ์คํํ๋ค.
* fetch()๋ฅผ ํ๋ฉด ์๋ฒ๋ก๋ถํฐ ์ ๋ฌ๋ฐ์ response ๊ฐ์ฒด๋ฅผ ๋ฆฌํดํ๋ค. ๋ฆฌํด๋ ๊ฐ์ฒด๋ then๋ฉ์๋์ ์ธ์๋ก ๋ฐ๋๋ค.
- ์๋ฒ๋ก๋ถํฐ response ๊ฐ์ฒด๋ฅผ ๋ฐ์ ๋, ๋ชจ๋ header๊ฐ ๋์ฐฉํ๋ฉด ๋ฐ๋ก returnํ๋ค.
=> ์์ง body๊ฐ ์ค์ง ์์ ์ํ, body๋ฅผ ๊ธฐ๋ค๋ฆฌ๊ณ ์๋ ์ํ์ธ Promise ๊ฐ์ฒด๋ฅผ ๋ฆฌํดํ๋ ๊ฒ์ด๋ค.
- ์ค๊ฐ์ ๊ธฐ๋ค๋ ค์ body๊น์ง ๋์ฐฉํ Promise ๊ฐ์ฒด๋ฅผ ๋ฆฌํดํ๋ ๊ฒ์ ๋ถ๊ฐ๋ฅํ๋ค. ์ ์ด์ header๊ฐ ๋์ฐฉํ์๋ง์ ๋ฐ๋ก ๋ฆฌํดํด๋ฒ๋ฆฌ๊ธฐ ๋๋ฌธ์, .then์ผ๋ก chaining(์ฒด์ด๋) ํ์ฌ body๊ฐ์ ๋ฐ์์ผ ํ๋ค.
Promise ๊ฐ์ฒด๋?
- ๋น๋๊ธฐ ํจ์ ํธ์ถ์ด ์๋ฃ๋์ ๋ ์ฒ๋ฆฌํ๊ธฐ ์ํ ํจ์๋ฅผ ์ค์ ํ๋ ๋ชจ๋
- ๋น๋๊ธฐ ์ฐ์ฐ์ ๊ฒฐ๊ณผ๋ฅผ ๋ํ๋ด๋ ๊ฐ์ฒด
- ์ฌ์ฉ์ด์ : ๊ธฐ์กด์ ์ฝ๋ฐฑํจ์ ์ฌ์ฉ ์ ์ฝ๋ฐฑ์ง์ฅ ๋ฐ์, ๊ฐ๋
์ฑ ๋จ์ด์ง๊ณ ์ ์ง๋ณด์์ฑ ๋ฎ์์.
Promise Chaining (์ฌ๋ฌ Promise ์ฐ๊ฒฐ)์ด๋?
- .then์ ๋์ดํ์ฌ ์ ์ดํ๋ ๋ฐฉ์.
- then์์ returnํด์ฃผ๊ณ ๋ค์ then์์ ์ด๋ฅผ ๋ฐ์ ์ฌ์ฉํ๋ ๋ฐฉ์.
2. Javascript ๊ฐ์ฒด์ JSON ๊ฐ์ฒด
- JSON.stringify() : Javascript => JSON ๊ฐ์ฒด๋ก ์ง๋ ฌํ
- JSON.parse() : JSON => Javascipt ๊ฐ์ฒด๋ก ์ง๋ ฌํ
'Web > Javascript' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Javascript] ๋ณ์, ์ฐ์ฐ์, ์กฐ๊ฑด๋ฌธ, ๋ฐ๋ณต๋ฌธ, ํจ์, ๋ฐฐ์ด (0) | 2023.12.17 |
---|