요청 구성
엔드포인트, HTTP method, headers, body, credentials 옵션을 요청 목적에 맞춰 고정한다.
Fetch API는 AJAX의 현대 문법일 뿐 아니라, Promise 기반으로 요청 생성·응답 해석·오류 분기를 명확히 나누는 네트워크 인터페이스다.
엔드포인트, HTTP method, headers, body, credentials 옵션을 요청 목적에 맞춰 고정한다.
fetch는 HTTP 404나 500에서도 reject하지 않으므로 status를 확인한다.
Content-Type에 맞춰 json, text, blob 등으로 body를 읽는다.
로딩, 성공, validation 오류, 네트워크 오류를 화면에서 구분한다.
const res = await fetch('/api/todos');
if (!res.ok) throw new Error(`HTTP ${res.status
overflow-wrap: break-word;
word-break: keep-all;
}`);
const todos = await res.json();