Unexpected token '<', "<!DOCTYPE "... is not valid JSON

  • JavaScript
  • Any API

What it means

Your code expected JSON data but received a web page. The request went to the wrong address, hit an error page, or was redirected to a login or 404 page.

Why it happens

  • The API address is wrong in production, so the host answers with its 404 page or your app's index.html.
  • The server crashed and returned an HTML error page.
  • The request was redirected to a login page because the user was not signed in.

How to fix it

  1. Open the Network tab, click the failing request and read its address, status and response.
  2. Check the API base address in your production settings.
  3. Check response.ok before calling response.json(), and show a real error otherwise.

Let your AI find the cause

Paste this into Claude Code, Cursor, Lovable or whichever AI built your app. It looks first and changes nothing until you have read the answer.

My app fails with 'Unexpected token <, <!DOCTYPE is not valid JSON'. Without changing anything yet, find the fetch call that fails, the exact address it requests in production, and what the server returns for it.