.serialize() encodes enabled form controls into name=value&... suitable for classic POST bodies. It is not JSON—many legacy endpoints expect urlencoded bodies.
JSON APIs
For REST JSON, build an object manually and JSON.stringify it; set contentType: 'application/json' in $.ajax. Mixing formats breaks CSRF tokens or server parsers.
Gotchas
- Unchecked checkboxes omitted—confirm server defaults
- File inputs need
FormData, not serialize - Duplicate names appear once—watch array conventions
Self-check
- When is serialize the right tool?
- What header pairs with JSON bodies?
Challenge
Serialize form
- Submit the mini form.
- Terminal shows query string from
.serialize().
Done when: serialize output matches form fields.
Tip: .serialize() skips unchecked boxes—mirror server expectations before refactoring endpoints.
Interview prep
- serialize vs JSON POST?
.serialize()builds application/x-www-form-urlencoded; JSON APIs needJSON.stringifyand correct Content-Type headers.