Send asynchronous browser requests with fetch, XMLHttpRequest, JSON, errors, and progressive UI.
Lessons
Attach bearer tokens to authorize requests.
AJAX with JWT is easiest to learn by reading the example, changing it, and observing the result.
const token = localStorage.getItem('token');
const res = await fetch('/api/profile', {
headers: { 'Authorization': 'Bearer ' + token }
});
if (res.status === 401) {
location.href = '/login';
} else {
console.log(await res.json());
}Practice the AJAX with JWT example in a small scratch file, then explain what changed and why.