async function logJSONData() {
const response = await fetch("http://example.com/movies.json");
const jsonData = await response.json();
console.log(jsonData);
}
fetch()
๋ ๊ฐ์ ธ์ค๊ณ ์ ํ๋ ๋ฆฌ์์ค์ ๊ฒฝ๋ก๋ฅผ ๋ํ๋ด๋ ์ธ์ 1๊ฐ๋ง ๋ฐ์์๋ต์ Response ๊ฐ์ฒด
body
bodyUsed
: ์๋ต ๋ณธ๋ฌธ์ด ์ฌ์ฉ๋๋์ง ์๋์ง๋ฅผ ๋ํ๋ด๋ ๋ถ๋ฆฌ์ธheaders
ok
: ์๋ต์ ์ฑ๊ณต ์ฌ๋ถ ๋ํ๋ด๋ ๋ถ๋ฆฌ์ธ(200-299)redirected
: ์๋ต์ด ๋ฆฌ๋๋ ์
์ ๊ฒฐ๊ณผ์ธ์งstatus
: ์๋ต ์ํ์ฝ๋statusText
: ์ํ ์ฝ๋์ ํด๋นํ๋ ์ํ ๋ฉ์์ง (ex. 200์ด๋ฉด โOKโ)type
: ์๋ต ์ ํ(ex. basic, cros)url
: ์๋ต URLerror()
: ๋คํธ์ํฌ ์ค๋ฅ์ ์ฐ๊ด๋ ์๋ก์ด Response ๊ฐ์ฒด ๋ฐํredirect()
: ๋ค๋ฅธ URL์ ๊ฐ๋ฆฌํค๋ ์๋ก์ด ์๋ต ์์ฑarrayBuffer()
: ์๋ต ๋ณธ๋ฌธ์ ArrayBuffer๋ก ์ดํํ๋ ํ๋ก๋ฏธ์ค ๋ฐํblob()
: ์๋ต ๋ณธ๋ฌธ์ Blob์ผ๋ก ์ดํํ๋ ํ๋ก๋ฏธ์ค ๋ฐํclone()
: Response ๊ฐ์ฒด ๋ณต์ฌ๋ณธ ์์ฑformData()
: ์๋ต ๋ณธ๋ฌธ์ FormData์ผ๋ก ์ดํํ๋ ํ๋ก๋ฏธ์ค ๋ฐํjson()
: ์๋ต ๋ณธ๋ฌธ์ JSON์ผ๋ก ํ์ฑํ ๊ฒฐ๊ณผ๋ก ์ดํํ๋ ํ๋ก๋ฏธ์ค ๋ฐํtext()
: ์๋ต ๋ณธ๋ฌธ์ text๋ก ์ดํํ๋ ํ๋ก๋ฏธ์ค ๋ฐํjson()
์ ์ฌ์ฉํด์ผ ํจfetch()
ํ๋ก๋ฏธ์ค๋ ๋คํธ์ํฌ ์ค๋ฅ๊ฐ ์์ ๋๋ง ๊ฑฐ๋ถ๋๋ฉฐ,
404 ๋ฑ HTTP ์ค๋ฅ ์์๋ reject ๋์ง ์์
โ then()
์ฒ๋ฆฌ๊ธฐ๋ ๋ฐ๋์ Response.ok
๋๋ Response.status
์์ฑ ํ์ธ
// POST ๋ฉ์๋ ๊ตฌํ ์์
async function postData(url = "", data = {}) {
// ์ต์
๊ธฐ๋ณธ ๊ฐ์ *๋ก ๊ฐ์กฐ
const response = await fetch(url, {
method: "POST", // *GET, POST, PUT, DELETE ๋ฑ
mode: "cors", // no-cors, *cors, same-origin
cache: "no-cache", // *default, no-cache, reload, force-cache, only-if-cached
credentials: "same-origin", // include, *same-origin, omit
headers: {
"Content-Type": "application/json",
// 'Content-Type': 'application/x-www-form-urlencoded',
},
redirect: "follow", // manual, *follow, error
referrerPolicy: "no-referrer", // no-referrer, *no-referrer-when-downgrade, origin, origin-when-cross-origin, same-origin, strict-origin, strict-origin-when-cross-origin, unsafe-url
body: JSON.stringify(data), // body์ ๋ฐ์ดํฐ ์ ํ์ ๋ฐ๋์ "Content-Type" ํค๋์ ์ผ์นํด์ผ ํจ
});
return response.json(); // JSON ์๋ต์ ๋ค์ดํฐ๋ธ JavaScript ๊ฐ์ฒด๋ก ํ์ฑ
}
postData("https://example.com/answer", { answer: 42 }).then((data) => {
console.log(data); // JSON ๋ฐ์ดํฐ๊ฐ `data.json()` ํธ์ถ์ ์ํด ํ์ฑ๋จ
});
fetch()
์ optionsmethod
: GET
, POST
๋ฑ ์์ฒญ ๋ฉ์๋
+) Origin
ํค๋๋ HEAD
๋ GET
๋ฉ์๋์ Fetch ์์ฒญ์์๋ ์ค์ ์ ๋จ
headers
: ์์ฒญ์ ์ถ๊ฐํ๊ณ ์ ํ๋ ํค๋๋ค
+) Authorization
HTTP ํค๋๋ ์์ฒญ์ ์ถ๊ฐํ ์ ์์ง๋ง, ์์ฒญ์ด ๋ค๋ฅธ ์ถ์ฒ๋ก ๋ฆฌ๋ค์ด๋ ํธ๋๋ฉด ์ ๊ฑฐ๋จ
body
: ์์ฒญ์ ์ถ๊ฐํ๊ณ ์ ํ๋ ๋ณธ๋ฌธ
mode
: ์์ฒญ์ ์ฌ์ฉํ ๋ชจ๋
cors
, no-cors
, same-origin
credentials
: ๋ธ๋ผ์ฐ์ ๊ฐ ์๊ฒฉ์ฆ๋ช
(์ฟ ํค, HTTP ์ธ์ฆ, TLS ํด๋ผ์ด์ธํธ ์ธ์ฆ์)์ ์ด๋ป๊ฒ ์ทจ๊ธํ ์ง ์ ์ด
omit
: ๋ธ๋ผ์ฐ์ ๊ฐ ์์ฒญ์์ ์๊ฒฉ์ฆ๋ช
์ ์ ์ธsame-origin
: ๋ธ๋ผ์ฐ์ ๊ฐ ๋์ผ ์ถ์ฒ URL ์์ฒญ์ ๋ํด์๋ ์๊ฒฉ์ฆ๋ช
์ ๋ณด๋ด๊ณ , ๋์ผ ์ถ์ฒ URL ์๋ต์ ํฌํจ๋ ์๊ฒฉ์ฆ๋ช
๋ ์ฌ์ฉํ๋๋ก ์ง์(๋ํดํธ)include
: ๋ธ๋ผ์ฐ์ ๊ฐ ๋์ผ๊ณผ ๊ต์ฐจ ์ถ์ฒ ์์ฒญ ๋ชจ๋์ ์๊ฒฉ์ฆ๋ช
์ ๋ณด๋cache
: ์์ฒญ์ด ๋ธ๋ผ์ฐ์ HTTP ์บ์์ ์ด๋ป๊ฒ ์ํธ์์ฉํ ์ง ์ ์ด
default
, no-store
, reload
, no-cache
, force-cache
, only-if-cached
redirect
: ๋ฆฌ๋ค์ด๋ ํธ ์์ฒญ ์ฒ๋ฆฌ๋ฒ
follow
: ์๋์ผ๋ก ๋ฆฌ๋ค์ด๋ ํธ ๋ฐ๋ผ๊ฐ(๋ํดํธ)error
: ๋ฆฌ๋ค์ด๋ ํธ ๋ฐ์ ์ ์ค๋ฅ์ ํจ๊ป ์์ฒญ ์ค๋จmanual
: ํธ์ถ์๊ฐ ์๋ต์ ๋ค๋ฅธ ์ปจํ
์คํธ์์ ์ฒ๋ฆฌํด์ผ ํจreferrer
: ์์ฒญ ๋ฆฌํผ๋ฌ๋ฅผ ์ง์ ํ๋ ๋ฌธ์์ด
referrerPolicy
no-referrer
, no-referrer-when-downgrade
, same-origin
, origin
, strict-origin
, origin-when-cross-origin
, strict-origin-when-cross-origin
, unsafe-url
integrity
keepalive
signal
AbortSignal
๊ฐ์ฒด ์ธ์คํด์คpriority
: ๊ฐ์ ์ข
๋ฅ์ ์ทจ๋ ์์ฒญ ์ค์์ ์ด ์์ฒญ์ ์๋์ ์ค์๋ ์ง์
high
, low
, auto
const myImage = document.querySelector("img");
const myRequest = new Request("flowers.jpg");
fetch(myRequest)
.then((response) => {
if (!response.ok) {
throw new Error(`HTTP ์ค๋ฅ! ์ํ: ${response.status}`);
}
return response.blob();
})
.then((response) => {
myImage.src = URL.createObjectURL(response);
});
Response.blob()
์ ํธ์ถํด ์ ํฉํ MIME ์ ํ ๋ถ์ฌURL.createObjectURL
๋ก url ์์ฑํด์ img ํ๊ทธ์ src๋ก ๋ฃ์ด์คconst myImage = document.querySelector("img");
const myHeaders = new Headers();
myHeaders.append("Accept", "image/jpeg");
const myInit = {
method: "GET",
headers: myHeaders,
mode: "cors",
cache: "default",
};
const myRequest = new Request("flowers.jpg");
fetch(myRequest, myInit).then((response) => {
// โฆ
});
append()
๋ฉ์๋๋ฅผ ํตํด ์์๋ฅผ ์ถ๊ฐํ ์ ์์