One Global Spinner for Every AJAX Call
Stop wiring loading states into every request. jQuery broadcasts global AJAX lifecycle events, and $.ajaxSetup injects headers everywhere - CSRF tokens included.
Web Applications Engineer
I build web applications and share what I learn along the way.
Step-by-step programming tutorials on PHP, JavaScript, Laravel, WordPress and more.
Stop wiring loading states into every request. jQuery broadcasts global AJAX lifecycle events, and $.ajaxSetup injects headers everywhere - CSRF tokens included.
new FormData(form) reads every field in one call, posts natively with fetch, and Object.fromEntries turns it into JSON. You may never write form.elements loops again.
fetch() still cannot report upload progress reliably - but XMLHttpRequest has done it for fifteen years. The full pattern: progress events, a PHP receiver, and the config limits that silently break big uploads.
Buttons work until the list refreshes over AJAX, then silently stop. Delegated events fix it permanently - and the same pattern is two lines in vanilla JS.
fetch waits forever by default. One static method gives every request a timeout, and combining signals cancels stale searches the moment the user keeps typing.
WebSockets need infrastructure. Polling hammers your server. SSE streams live updates over normal HTTP with a PHP file and the built-in EventSource API - reconnection included.
onLine only knows if there is a network interface, not whether the internet works. Real offline handling means verified checks and a retry queue for failed writes.
Async generators hide the pagination bookkeeping - cursors, hasMore flags, page counters - behind a clean loop that fetches pages only as you consume them.
Fire five parallel AJAX requests at a PHP backend and watch them execute one... at... a... time. The culprit is the session file lock, and the fix is a single function call.
No cURL, no Guzzle, no problem: stream contexts give file_get_contents methods, headers, bodies, timeouts - plus a magic variable that captures response headers.
Promise.all fails everything when one request dies - usually not what a dashboard wants. The other three combinators cover the cases all() gets wrong.
Search-as-you-type touches four failure modes at once - request spam, stale responses, LIKE wildcards and unbounded scans. The complete pattern, both sides of the wire.