Send asynchronous browser requests with fetch, XMLHttpRequest, JSON, errors, and progressive UI.
Lessons
Understand OPTIONS preflight and allowed headers.
CORS Preflight Requests is easiest to learn by reading the example, changing it, and observing the result.
// A request with custom headers or PUT/DELETE triggers a preflight.
// The browser first sends an OPTIONS request:
//
// OPTIONS /api/lessons
// Access-Control-Request-Method: PUT
// Access-Control-Request-Headers: content-type, authorization
//
// The server must reply with matching Access-Control-Allow-* headers
// before the real request is sent.
fetch('/api/lessons/1', {
method: 'PUT',
headers: { 'Content-Type': 'application/json', 'Authorization': 'Bearer x' },
body: JSON.stringify({ title: 'Updated' })
});Practice the CORS Preflight Requests example in a small scratch file, then explain what changed and why.