Web development
Serve files with the correct Content-Type
Set Content-Type to describe the bytes a response actually contains. A JavaScript or JSON URL that returns an HTML fallback can fail even though the request status is 200.
Worked example
Worked reference example
# JSON response
Content-Type: application/json
# HTML response
Content-Type: text/html; charset=utf-8
# CSS response
Content-Type: text/cssHow it works
The browser uses media type information to interpret a response and apply relevant security checks. Inspecting the body alongside the header often reveals a missing-file fallback, login page, or hosting rewrite that returned the wrong representation.
Limits and mistakes
A filename extension alone does not guarantee the server emits the correct media type. Do not solve a script error by forcing every response to JavaScript. Fix routing and asset paths so each URL returns the actual intended file.
Check the result
Request the failing resource directly and inspect its first bytes, status, and header. A supposed JSON response beginning with an HTML doctype usually indicates a routing or authentication problem.
Sources
Reference documentation for the behavior used in this guide: