Fetch APIë HTTP íì´íë¼ì¸ì 구ì±íë ìì²ê³¼ ìëµ ë±ì ìì를 JavaScriptìì ì ê·¼íê³ ì¡°ìí ì ìë ì¸í°íì´ì¤ë¥¼ ì ê³µí©ëë¤. Fetch APIê° ì ê³µíë ì ì fetch()
ë©ìëë¡ ë¤í¸ìí¬ì 리ìì¤ë¥¼ ì½ê² ë¹ë기ì ì¼ë¡ ì·¨ëí ìë ììµëë¤.
ì½ë°± ê¸°ë° APIì¸ XMLHttpRequest
ì ë¬ë¦¬, Fetch APIë ìë¹ì¤ ì커ììë ì½ê² ì¬ì©í ì ìë íë¡ë¯¸ì¤ 기ë°ì ê°ì ë ëì²´ì ì
ëë¤. ëí Fetch APIë CORS를 í¬í¨í ê³ ê¸ ê°ë
ì HTTP íì¥ì¼ë¡ ì ìí©ëë¤.
기본ì ì¸ ë¦¬ìì¤ ì·¨ë ìì²ì ì´ë ê² ìê²¼ìµëë¤.
async function logJSONData() {
const response = await fetch("http://example.com/movies.json");
const jsonData = await response.json();
console.log(jsonData);
}
ì ì½ëë ë¤í¸ìí¬ë¥¼ íµí´ JSON íì¼ì ì·¨ëí´ì ì½ìì ì¶ë ¥í©ëë¤. ê°ì¥ ë¨ìí ííì fetch()
ë ê°ì ¸ì¤ê³ ì íë 리ìì¤ì ê²½ë¡ë¥¼ ëíë´ë íëì ì¸ìë§ ë°ìµëë¤. ìëµì Response
ê°ì²´ë¡ ííëë©°, JSON ìëµ ë³¸ë¬¸ì ë°ë¡ ë°ííì§ë ììµëë¤.
Response
ê°ì²´ ìì JSON ìëµ ë³¸ë¬¸ì ê·¸ëë¡ í¬í¨íì§ë ììµëë¤. Response
ë HTTP ìëµ ì 체를 ëíë´ë ê°ì²´ë¡, JSON 본문 ì½í
ì¸ ë¥¼ ì¶ì¶í기 ìí´ìë json()
ë©ìë를 í¸ì¶í´ì¼ í©ëë¤. json()
ì ìëµ ë³¸ë¬¸ í
ì¤í¸ë¥¼ JSONì¼ë¡ íì±í ê²°ê³¼ë¡ ì´ííë, ë ë¤ë¥¸ íë¡ë¯¸ì¤ë¥¼ ë°íí©ëë¤.
ì°¸ê³ : ë¤ë¥¸ ì íì 본문 ì½í ì¸ ë¥¼ ì¶ì¶í ì ìë ë¹ì·í ë©ìëë¤ì 본문 ì ìì íì¸íì¸ì.
ì·¨ë ìì²ì ê°ì ¸ì¤ë ¤ë 리ìì¤ì ì§ìë¬¸ì´ ìë, Content-Security-Policy
í¤ëì connect-src
ì§ì문ì ìí´ ì ì´ë©ëë¤.
fetch()
ë©ìëìë ì íì ì¼ë¡ ë ë²ì§¸ 매ê°ë³ìë ì ê³µí ì ììµëë¤. ì´ ë§¤ê°ë³ì, init
ê°ì²´ë¥¼ ì¬ì©íë©´ ì¬ë¬ê°ì§ ì¤ì ì ì ì´í ì ììµëë¤.
fetch()
문ì를 ë°©ë¬¸í´ ì¬ì© ê°ë¥í ì ì²´ ìµì
ì 목ë¡ê³¼, ê°ê°ì ìµì
ì ëí ìì¸í ì¤ëª
ì íì¸íì¸ì.
// 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()` í¸ì¶ì ìí´ íì±ë¨
});
mode: "no-cors"
를 ì§ì íë©´ ìì²ì ì¬ì©í ì ìë í¤ëê° ë¤ì 목ë¡ì¼ë¡ ì íë©ëë¤.
Accept
Accept-Language
Content-Language
Content-Type
, ê°ì¼ë¡ë application/x-www-form-urlencoded
, multipart/form-data
, ëë text/plain
ìì§ ìë£ëì§ ìì fetch()
ìì
ì ì·¨ìíë ¤ë©´ AbortController
ì AbortSignal
ì¸í°íì´ì¤ë¥¼ ì¬ì©íì¸ì.
const controller = new AbortController();
const signal = controller.signal;
const url = "video.mp4";
const downloadBtn = document.querySelector("#download");
const abortBtn = document.querySelector("#abort");
downloadBtn.addEventListener("click", async () => {
try {
const response = await fetch(url, { signal });
console.log("ë¤ì´ë¡ë ìë£", response);
} catch (error) {
console.error(`ë¤ì´ë¡ë ì¤ë¥: ${error.message}`);
}
});
abortBtn.addEventListener("click", () => {
controller.abort();
console.log("ë¤ì´ë¡ë ì¤ë¨ë¨");
});
ì격 ì¦ëª
ì í¬í¨í ìì² ì ì¡
ë¸ë¼ì°ì ê° ìì²ì ì ì¡í ë ì격 ì¦ëª
ì í¬í¨íëë¡ íë ¤ë©´ fetch()
ë©ìëì ì ë¬íë init
ê°ì²´ì credential: 'include'
를 ì¶ê°íì¸ì. ëì¼ ì¶ì²ì êµì°¨ ì¶ì² ìì² ëª¨ëì ì¬ì©í ì ììµëë¤.
fetch("https://example.com", {
credentials: "include",
});
ì°¸ê³ :
credentials: 'include'
를 ì¶ê°í ê²½ì°,Access-Control-Allow-Origin
ì ìì¼ëì¹´ë를 ì¬ì©í ì ììµëë¤. ì격 ì¦ëª ì í¬í¨íë ¤ë ê²½ì°ìë ë°ëì ì íí ì¶ì²ë¥¼ ì§ì í´ì¼ í©ëë¤. CORS í´ì íì¥ íë¡ê·¸ë¨ì ì¬ì©íëë¼ë ìì¼ëì¹´ë를 ì§ì í ìì²ì ì¤í¨í ê²ì ëë¤.
ì°¸ê³ : ì격 ì¦ëª ìµì ì ê°ì ìê´ ìì´, ë¸ë¼ì°ì ë í리íë¼ì´í¸ ìì²ìë ì격 ì¦ëª ì ì ì¡íì§ ììì¼ í©ëë¤. ìì¸í ì ë³´ë ì격 ì¦ëª ì í¬í¨í CORS ìì²ì ì°¸ê³ íì¸ì.
ìì² URLì´ ì¤í¬ë¦½í¸ì ê°ì ì¶ì²ì¼ ëë§ ì격 ì¦ëª
ì ì ì¡íë ¤ë©´ credentials: 'same-origin'
ì ì¶ê°íì¸ì.
// ì¤í¬ë¦½í¸ì ì¶ì²ë 'https://example.com'
fetch("https://example.com", {
credentials: "same-origin",
});
ë¸ë¼ì°ì ê° ìì²ìì ì격 ì¦ëª
ì ì ì¡íì§ ìëë¡ íë ¤ë©´ credentials: 'omit'
ì ì¬ì©íì¸ì.
fetch("https://example.com", {
credentials: "omit",
});
JSON ë°ì´í° ì
ë¡ë
fetch()
를 ì¬ì©íë©´ JSON ì¸ì½ë©ë ë°ì´í°ë¥¼ POST ìì²ì í¬í¨í ì ììµëë¤.
async function postJSON(data) {
try {
const response = await fetch("https://example.com/profile", {
method: "POST", // ëë 'PUT'
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(data),
});
const result = await response.json();
console.log("ì±ê³µ:", result);
} catch (error) {
console.error("ì¤í¨:", error);
}
}
const data = { username: "example" };
postJSON(data);
íì¼ ì
ë¡ë
fetch()
ì <input type="file">
ì
ë ¥ ìì, FormData()
를 ì¬ì©í´ì íì¼ì ì
ë¡ëí ì ììµëë¤.
async function upload(formData) {
try {
const response = await fetch("https://example.com/profile/avatar", {
method: "PUT",
body: formData,
});
const result = await response.json();
console.log("ì±ê³µ:", result);
} catch (error) {
console.error("ì¤í¨:", error);
}
}
const formData = new FormData();
const fileField = document.querySelector('input[type="file"]');
formData.append("username", "abc123");
formData.append("avatar", fileField.files[0]);
upload(formData);
ë¤ìì íì¼ ì
ë¡ë
fetch()
ì <input type="file" multiple>
ì
ë ¥ 칸 ìì, FormData()
를 ì¬ì©í´ì ì¬ë¬ íì¼ì ì
ë¡ëí ì ììµëë¤.
async function uploadMultiple(formData) {
try {
const response = await fetch("https://example.com/posts", {
method: "POST",
body: formData,
});
const result = await response.json();
console.log("ì±ê³µ:", result);
} catch (error) {
console.error("ì¤í¨:", error);
}
}
const photos = document.querySelector('input[type="file"][multiple]');
const formData = new FormData();
formData.append("title", "My Vegas Vacation");
for (const [i, photo] of Array.from(photos.files).entries()) {
formData.append(`photos_${i}`, photo);
}
uploadMultiple(formData);
í
ì¤í¸ íì¼ì í ì¤ì© ì²ë¦¬í기
ìëµìì ì½ì´ì¤ë ë°ì´í° ì²í¬ë ì¤ ë¨ìë¡ ê¹ëíê² ëëì§ ìì¼ë©°, 문ìì´ë ìëê³ Uint8Array
ì
ëë¤. í
ì¤í¸ íì¼ì ê°ì ¸ìì ì¤ ë¨ìë¡ ì²ë¦¬íê³ ì íë¤ë©´, 'ì¤' ë¨ìë¡ ëëë ìì
ì ì§ì 구íí´ì¼ í©ëë¤. ì´ ìì ë ì¤ ë¨ì ë°ë³µê¸°ë¥¼ ìì±í´ì íì¼ì ì²ë¦¬íë ììì
ëë¤. (ë무 ë³µì¡í´ì§ì§ ìëë¡ í
ì¤í¸ íì¼ì UTF-8ë¡ ê°ì íê³ , ë¤í¸ìí¬ ì¤ë¥ë ê³ ë ¤íì§ ììµëë¤.)
async function* makeTextFileLineIterator(fileURL) {
const utf8Decoder = new TextDecoder("utf-8");
const response = await fetch(fileURL);
const reader = response.body.getReader();
let { value: chunk, done: readerDone } = await reader.read();
chunk = chunk ? utf8Decoder.decode(chunk) : "";
const re = /\n|\r|\r\n/gm;
let startIndex = 0;
let result;
for (;;) {
let result = re.exec(chunk);
if (!result) {
if (readerDone) {
break;
}
let remainder = chunk.substr(startIndex);
({ value: chunk, done: readerDone } = await reader.read());
chunk = remainder + (chunk ? utf8Decoder.decode(chunk) : "");
startIndex = re.lastIndex = 0;
continue;
}
yield chunk.substring(startIndex, result.index);
startIndex = re.lastIndex;
}
if (startIndex < chunk.length) {
// ë§ì§ë§ ì¤ì´ ê°í 문ìë¡ ëëì§ ììì ë
yield chunk.substr(startIndex);
}
}
async function run() {
for await (let line of makeTextFileLineIterator(urlOfFile)) {
processLine(line);
}
}
run();
ì·¨ë ì±ê³µ ì¬ë¶ íì¸
fetch()
íë¡ë¯¸ì¤ë ë¤í¸ìí¬ì ì¤ë¥ê° ììê±°ë, ìë²ì CORS ì¤ì ì´ ì못ë ê²½ì° TypeError
ë¡ ê±°ë¶ë©ëë¤. ê·¸ë¬ë ì´ ë ê²½ì°ë ê¶íì²ë¼ ì¤ì ì 문ì ê³ , 404ì ê°ì ìëµì ë¤í¸ìí¬ ì¤ë¥ê° ìëë¯ë¡ ê±°ë¶íì§ ììµëë¤. fetch()
ê° ì±ê³µíëì§ë¥¼ ì íí ììë´ë ¤ë©´ íë¡ë¯¸ì¤ì ì´í ì¬ë¶ë¥¼ íì¸í í, Response.ok
ìì±ì ê°ì´ true
ì¸ì§ë íì¸í´ì¼ í©ëë¤.
async function fetchImage() {
try {
const response = await fetch("flowers.jpg");
if (!response.ok) {
throw new Error("ë¤í¸ìí¬ ìëµì´ OKê° ìë");
}
const myBlob = await response.blob();
myImage.src = URL.createObjectURL(myBlob);
} catch (error) {
console.error("ì·¨ëì 문ì ê° ìììµëë¤:", error);
}
}
ìì² ê°ì²´ë¥¼ ì§ì ì ê³µ
fetch()
í¸ì¶ì 리ìì¤ì ê²½ë¡ë¥¼ ì ê³µíë ëì , Request()
ìì±ìë¡ ìì±í ìì² ê°ì²´ë¥¼ ì¸ìë¡ ì ë¬í ìë ììµëë¤.
async function fetchImage(request) {
try {
const response = await fetch(request);
if (!response.ok) {
throw new Error("ë¤í¸ìí¬ ìëµì´ OKê° ìë");
}
const myBlob = await response.blob();
myImage.src = URL.createObjectURL(myBlob);
} catch (error) {
console.error("ì¤ë¥:", error);
}
}
const myHeaders = new Headers();
const myRequest = new Request("flowers.jpg", {
method: "GET",
headers: myHeaders,
mode: "cors",
cache: "default",
});
fetchImage(myRequest);
Request()
ìì±ìë fetch()
ë©ìëì ëì¼í 매ê°ë³ì를 ë°ìµëë¤. 기존ì ì¡´ì¬íë ìì² ê°ì²´ë¥¼ ì ë¬í´ì ë³µì¬ë³¸ì ìì±íë ê²ë ê°ë¥í©ëë¤.
const anotherRequest = new Request(myRequest, myInit);
ìì²ê³¼ ìëµ ë³¸ë¬¸ì í ë²ë§ ì½ì ì ìì¼ë¯ë¡ ë³µì¬ë³¸ ìì±ì 꽤 ì ì©í©ëë¤. ì´ë° ìì¼ë¡ ë³µì¬ë³¸ì ìì±íë©´, 기존ì ìì±í´ë ìì²/ìëµ ê°ì²´ë¥¼ ë¤ì ì¬ì©íë init
ìµì
ë§ êµì²´í ìë ììµëë¤. ë³µì¬ë³¸ì ì본ì 본문ì ì½ê¸° ì ì ìì±í´ì¼ í©ëë¤.
ì°¸ê³ : ë³µì¬ë³¸ ìì±ë§ì ìí clone()
ë©ìëë ììµëë¤. ìì±ìì ì´ ë©ìë 모ë, ì´ë¯¸ 본문ì ì½ì ìì² ëë ìëµì ë³µì¬íë ¤ê³ ìëíë©´ ì¤í¨í©ëë¤. ë³µì¬ë³¸ì 본문ì ì½ì´ë ì본ìë ìí¥ì ì£¼ì§ ììµëë¤.
Headers
ì¸í°íì´ì¤ì Headers()
ìì±ì를 ì¬ì©í´ì ìì ë§ì í¤ë ê°ì²´ë¥¼ ìì±í ì ììµëë¤. í¤ë ê°ì²´ë ì´ë¦ê³¼ ê°ì ì°ê²°íë ê°ë¨í ë§µì
ëë¤.
const content = "Hello World";
const myHeaders = new Headers();
myHeaders.append("Content-Type", "text/plain");
myHeaders.append("Content-Length", content.length.toString());
myHeaders.append("X-Custom-Header", "ProcessThisImmediately");
ìì±ìì 2ì°¨ì ë°°ì´ì´ë ê°ì²´ 리í°ë´ì ì ë¬íë ê²ì¼ë¡ë ê°ì 결과를 ì»ì ì ììµëë¤.
const myHeaders = new Headers({
"Content-Type": "text/plain",
"Content-Length": content.length.toString(),
"X-Custom-Header": "ProcessThisImmediately",
});
í¤ëì ë´ì©ì ìëì ê°ì´ ê°ì ¸ì¬ ì ììµëë¤.
console.log(myHeaders.has("Content-Type")); // true
console.log(myHeaders.has("Set-Cookie")); // false
myHeaders.set("Content-Type", "text/html");
myHeaders.append("X-Custom-Header", "AnotherValue");
console.log(myHeaders.get("Content-Length")); // 11
console.log(myHeaders.get("X-Custom-Header")); // ['ProcessThisImmediately', 'AnotherValue']
myHeaders.delete("X-Custom-Header");
console.log(myHeaders.get("X-Custom-Header")); // null
í¤ë ê°ì²´ì ëªëª ìì ì ìë¹ì¤ ì커ììë ì ì©íì§ë§, ê·¸ëë í¨ì¬ í¸í API를 íµí´ì í¤ë를 ì¡°ìí ì ììµëë¤.
Headers
ì 모ë í¤ë ë©ìëë ì í¨íì§ ìì HTTP í¤ë ì´ë¦ì ë°ìì ë TypeError
를 ëì§ëë¤. ë³ê²½ ë©ìëë ë¶ë³ ë³´í¸(ìë ì°¸ê³ )ê° ì¡´ì¬íë©´ TypeError
를 ëì§ëë¤. ê·¸ ì¸ìë ì¤í¨í ë ì¡°ì©íê² ì¤í¨í©ëë¤. ë¤ìì ì¡°ì©í ì¤í¨ ì½ëì ììì
ëë¤.
const myResponse = Response.error();
try {
myResponse.headers.set("Origin", "http://mybank.com");
} catch (e) {
console.log("ìíì¸ ì² í ì ìì´ì!");
}
ì½í ì¸ ë¥¼ íì±í기 ì ì ì í¨í íìì¸ì§ íì¸í ë í¤ë ê°ì²´ë¥¼ ì ì©íê² ì¬ì©í ì ììµëë¤. ë¤ìì ê·¸ ììì ëë¤.
async function fetchJSON(request) {
try {
const response = await fetch(request);
const contentType = response.headers.get("content-type");
if (!contentType || !contentType.includes("application/json")) {
throw new TypeError("ì, JSONì´ ìëëë¤!");
}
const jsonData = await response.json();
// ë°ì´í° ì¶ê° ê°ê³µ
} catch (error) {
console.error("ì¤ë¥:", error);
}
}
ê°ë
í¤ëë ìì²ì¼ë¡ ì ì¡í ìë ìê³ ìëµì¼ë¡ ë°ì ìë ìì¼ë©° ì´ë¤ ì 보를 ìì í ì ìê³ ìì í ì ìëì§ ë¤ìí ì íì´ ì¡´ì¬í기 ë문ì, í¤ë ê°ì²´ë 'ê°ë'(guard) ìì±ì ê°ìµëë¤. ê°ë ìì±ì ì¹ì ë ¸ì¶ëì§ ìì§ë§, í¤ë ê°ì²´ì íì©ëë ë³ê²½ ìì ì ë²ìì ìí¥ì ì¤ëë¤.
ê°ë¥í ê°ë ê°ì ë¤ìê³¼ ê°ìµëë¤.
none
: 기본 ê°ì
ëë¤.request
: ìì²(Request.headers
)ìì íëí í¤ë ê°ì²´ë¥¼ ë³´í¸í©ëë¤.request-no-cors
: Request.mode
ê° no-cors
ì¸ ìì²ìì íëí í¤ë ê°ì²´ë¥¼ ë³´í¸í©ëë¤.response
: ìëµ(Response.headers
)ìì íëí í¤ë ê°ì²´ë¥¼ ë³´í¸í©ëë¤.immutable
: í¤ë ê°ì²´ë¥¼ ì½ê¸° ì ì©ì¼ë¡ ì¤ì í©ëë¤. ëë¶ë¶ ìë¹ì¤ ì커ìì ì¬ì©í©ëë¤.ì°¸ê³ : ê°ëê° ì ì©ë ìëµ í¤ëìë Content-Length
í¤ë를 ì¶ê°íê±°ë ì¤ì í ì ììµëë¤. ë§ì°¬ê°ì§ë¡,Set-Cookie
í¤ë ëí ìëµì ì¶ê°í ì ììµëë¤. ì¦, ìë¹ì¤ ì커ìì ìëµì í©ì±í´ì ì¿ í¤ë¥¼ ì¤ì íë ê²ì ë¶ê°ë¥í©ëë¤.
ììì 본 ë°ì ê°ì´ Response
ì¸ì¤í´ì¤ë fetch()
íë¡ë¯¸ì¤ê° ì´íí ë ë°íë©ëë¤.
ë¤ìì ìëµ ê°ì²´ìì ìë§ ê°ì¥ ë§ì´ ì¬ì©íê² ë ìì±ë¤ì ëë¤.
Response.status
â ìí ì½ë ê°ì ë´ì ì ì ê°ì
ëë¤. 기본 ê°ì 200ì
ëë¤.Response.statusText
â ìí ì½ë ë©ìì§ë¥¼ ë´ì 문ìì´ ê°ì
ëë¤. 기본 ê°ì ë¹ ë¬¸ìì´ì
ëë¤. ì°¸ê³ ë¡ HTTP/2ë ìí ë©ìì§ë¥¼ ì§ìíì§ ììµëë¤.Response.ok
â ì쪽 ìì ìì ì¬ì©íë¯, ìí ì½ëê° 200 ì´ì 299 ì´íì¸ì§ ê°ë¨íê² íì¸í ì ìë ë¶ë¦¬ì¸ ê°ì
ëë¤.ìëµ ê°ì²´ë JavaScriptìì ì§ì ìì±í ìë ìì§ë§, ì´ë° ìëµì ìì í ìì²ì ëí´ respondWith()
ë©ìëë¡ ì§ì ìëµí´ì¼ íë ìë¹ì¤ ì커ììë íì½í ì ììµëë¤.
const myBody = new Blob();
addEventListener("fetch", function (event) {
// fetch를 ê°ë¡ì±ë ServiceWorker
event.respondWith(
new Response(myBody, {
headers: { "Content-Type": "text/plain" },
}),
);
});
Response()
ìì±ìë ë ê°ì ì íì ì¸ì를 ë°ìµëë¤. íëë ìëµ ë³¸ë¬¸ì¼ë¡ ì°ê³ , ë¤ë¥¸ íëëRequest()
ê° ë°ë ê²ê³¼ ë¹ì·í ìµì
ê°ì²´ì
ëë¤.
ë³¸ë¬¸ì°¸ê³ :
error()
ì ì ë©ìëë ì¤ë¥ ìëµì ë°íí©ëë¤. ë§ì°¬ê°ì§ë¡,redirect()
ë ì§ì í URLë¡ ë¦¬ë¤ì´ë í¸ë¥¼ ì ë°íë ìëµì ìì±í©ëë¤. ì´ ë ë©ìë ìì ìë¹ì¤ ì커ììë§ ìë¯¸ê° ììµëë¤.
ìì²ê³¼ ìëµ ëª¨ë 본문 ë°ì´í°ë¥¼ í¬í¨í ì ììµëë¤. 본문 ë°ì´í°ë ìë 목ë¡ì ì í ì¤ íëì ì¸ì¤í´ì¤ì ëë¤.
ArrayBuffer
TypedArray
(Uint8Array ë±ë±)DataView
Blob
File
String
ëë 문ìì´ ë¦¬í°ë´URLSearchParams
FormData
Request
ì Response
ì¸í°íì´ì¤ë 본문ì ì¶ì¶í ì ìë ë¤ìì ë©ìëë¤ì ê³µì í©ëë¤. ì¶ì¶ ë©ìëë 모ë íë¡ë¯¸ì¤ë¥¼ ë°ííë©°, ì´ íë¡ë¯¸ì¤ê° ì¤ì 본문 ë°ì´í°ë¡ ì´íí©ëë¤.
Request.arrayBuffer()
/ Response.arrayBuffer()
Request.blob()
/ Response.blob()
Request.formData()
/ Response.formData()
Request.json()
/ Response.json()
Request.text()
/ Response.text()
본문 ì¶ì¶ ë©ìëë¤ì ì¬ì©íë©´ XHRì ì¬ì©í ëë³´ë¤ ë ì½ê² ë¹ í ì¤í¸ ë°ì´í°ë¥¼ ì²ë¦¬í ì ììµëë¤.
ìì² ë³¸ë¬¸ì body
ìì±ì ì¤ì íë ê²ì¼ë¡ ì¶ê°í ì ììµëë¤.
const form = new FormData(document.getElementById("login-form"));
fetch("/login", {
method: "POST",
body: form,
});
ìì²ê³¼ ìëµ, ê·¸ë¦¬ê³ ë ëìê° fetch()
í¨ìë 본문ì ë³´ê³ ì½í
ì¸ ì íì ììë´ë ¤ê³ ìëí©ëë¤. ëí, ìì²ì ë°ë¡ ëª
ìíì§ ììì¼ë©´ Content-Type
ì í¤ë를 ìëì¼ë¡ ì¤ì í©ëë¤.
Fetch APIë Window
ë Worker
ì¤ì½íì Headers
, Request
, Response
, ëë fetch()
ì ì¡´ì¬ ì¬ë¶ë¡ ì§ì ì¬ë¶ë¥¼ íì¸í ì ììµëë¤.
if (window.fetch) {
// fetchë¡ ìì² ë³´ë´ê¸°
} else {
// XMLHttpRequest ì¬ì©í기?
}
ê°ì´ 보기
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4