Send asynchronous browser requests with fetch, XMLHttpRequest, JSON, errors, and progressive UI.
Lessons
Attach auth tokens and handle errors globally.
Axios Interceptors is easiest to learn by reading the example, changing it, and observing the result.
import axios from 'axios';
const api = axios.create({ baseURL: '/api' });
// Attach a token to every request
api.interceptors.request.use(config => {
config.headers.Authorization = 'Bearer ' + localStorage.token;
return config;
});
// Handle errors globally
api.interceptors.response.use(
res => res,
err => {
if (err.response?.status === 401) location.href = '/login';
return Promise.reject(err);
}
);Practice the Axios Interceptors example in a small scratch file, then explain what changed and why.