🔎 What you'll see
SyntaxError: Unexpected token '<', "<!DOCTYPE "... is not valid JSON
🤔 Why it happens
Your code expected JSON data, but the server sent back HTML (usually a 404/500 error page or a login page). The "<" in the error message is the start of that HTML.
🛠️ How to fix it
- Paste the request URL into the address bar. If you see an HTML page, the address is wrong or the server has an error.
- Check the API URL for typos, a missing slash (/) or a wrong path.
- On your live server, the API path may differ from development (e.g. there's no /api).
- Add code that checks response.ok and status from fetch before reading the JSON.
🤖 What to ask your AI
Replace the parts in [ ] with your own details, then paste it in.
I'm fetching JSON but get an 'Unexpected token <' error. The request URL is [URL] and my code is [code]. Tell me how to track down the cause, and give me code that checks the response status first.
Other errors
CORS errorThe browser blocks your page from fetching data from another site404 Not FoundThe address or file can't be foundundefined / Cannot read properties of undefinedReading a property of a value that isn't thereis not definedUsing a name that was never declaredSyntaxErrorBrackets or quotes don't match upExposed API keyYou put a secret key in your code or on GitHubMixed ContentAn https page loads something from an http addressBlank white pageYou deployed it and all you see is a blank screenis not a functionCalling something that isn't a functionCannot use import statement outside a moduleThe browser won't accept your import statementfavicon.ico 404The console shows a 404 for favicon.icoData disappears when I refreshYour to-dos or scores aren't being savedPage looks broken or tiny on phonesFine on a computer, weird on mobilegit push rejectedA "rejected" / non-fast-forward errornpm: command not foundYour terminal says npm or node doesn't existPort 3000 is already in useYour dev server says the port is takenEnvironment variables (.env) aren't loadingYou put your API key in .env but it comes out undefinedThe AI keeps failing to fix the same errorYou ask for a fix and nothing changes