Unexpected token '<' usually means JavaScript tried to parse a response as JSON, but the response began with HTML markup instead. The error points to a mismatch between the format your code expected and the body it received; by itself, it does not tell you which server, route, proxy, or other component supplied that HTML.
What the error means
JSON.parse() accepts text that follows JSON grammar. If the text does not, JavaScript throws a SyntaxError; Response.json() fails too when the response body cannot be parsed as JSON. A reported < often points to a body that starts with an HTML doctype or tag, such as an error page or a frontend page returned in place of API data. MDN’s JSON.parse() documentation describes the parsing requirement, while its guide to Unexpected token errors explains that the message alone does not identify a unique cause.
This is a clue, not proof that a particular API server generated the HTML. The body might have come from routing, authentication or redirect handling, a frontend fallback, a proxy or gateway, or a server error handler.
Why fetch can succeed while JSON parsing fails
A fulfilled fetch() promise means a Response was received; it does not guarantee an HTTP success status or a JSON body. For example, an HTTP 404 still produces a response. MDN’s Using the Fetch API guide advises checking the response status as well as handling fetch errors.
So there are two separate questions: did the request receive an acceptable HTTP status, and does its body match the format the code expects? Check response.ok or response.status for the first, and the response’s Content-Type and body for the second.
Do these 3 things before closing this tab:
1Clear out junk files and repair common Windows errors2Scan for outdated or missing drivers - takes under a minute3Repair Windows errors before they cause bigger problems#1 Best Overall
How to find where the HTML came from
- Find the request. In your browser’s Network panel, select the failing request and confirm its URL and method. Make sure it is the API endpoint you intended to call.
- Check the status and final URL. A 404 or another non-OK status may point to a wrong route or server-side error. A changed final URL can help reveal redirect handling, such as a request ending at a sign-in or frontend page.
- Check
Content-Type. If it is not a JSON media type, do not assumeresponse.json()can parse the body as JSON. Some JSON APIs use vendor media types, includingapplication/problem+json, so account for the media types your API documents rather than checking only for one exact string. - Preview the body as text. A short preview can show whether the response is an HTML page, a plain-text error, or another representation. Avoid logging sensitive response bodies in production.
- Trace the layer that returned it. Use the URL, status, headers, and body together to investigate routing, authentication or redirect handling, a frontend fallback, a proxy or gateway, or a server error handler. The parse error alone cannot distinguish among these possibilities.
Handle HTTP errors and unexpected bodies separately
This illustrative pattern checks the HTTP status and media type before parsing. It reads the body as text once so it can include a short preview in an error; a response body cannot be consumed twice, so the success path parses that saved text rather than calling response.json() afterward.
async function getJson(url) {
const response = await fetch(url);
const contentType = response.headers.get("content-type") ?? "";
const body = await response.text();
if (!response.ok) {
throw new Error(`HTTP ${response.status} for ${url}`);
}
const isJson = /(?:application|[^;]++json)/i.test(contentType);
if (!isJson) {
const preview = body.slice(0, 200);
throw new TypeError(`Expected JSON, received ${contentType}: ${preview}`);
}
return JSON.parse(body);
}
This is a starting point, not a universal API client. Adapt media-type checks and error handling to your API, and redact secrets before placing body previews in logs or error reports. Keep HTTP-status failures distinct from JSON syntax failures: the former indicates an unsuccessful response, while the latter means the body could not be parsed as JSON.
Rank #2
- Used Book in Good Condition
What will not fix it
Changing JSON parsing options cannot turn an HTML error page into the API data your application intended to receive. First correct the request path or the server-side behavior that returned the wrong representation; then parse the response that endpoint is meant to provide.
Quick Recap
Best Value
Rank #4
Rank #3
Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.




