Arama Yap Mesaj Submit
Request a Callback
+90
X
X

Select Your Currency

Turkish Lira $ US Dollar Euro
X
X

Select Your Currency

Turkish Lira $ US Dollar Euro

Contact Us

Location Halkali merkez neighborhood fatih st ozgur apt no 46 , Kucukcekmece , Istanbul , 34303 , TR
Fetch, AJAX, and API Responses

Unexpected Token `<` in JSON at Position 0 Error

The first character being '<' usually indicates that the expected JSON response is HTML. This could be due to a wrong endpoint, a 404/500 error page, a login redirect, a PHP warning, a WAF challenge, or a WISECP theme output being attempted to be parsed like JSON by response.json().

JSON Position 0<!DOCTYPE html>Fetch APIPHP AJAXContent-Type
DevTools Console
SyntaxError: Unexpected token '<', "<!DOCTYPE "... is not valid JSON
Unexpected token < in JSON at position 0
JSON.parse: unexpected character at line 1 column 1
01Log first Console error
02Separate source code and live answer
03Test Parser, Network and module structure
04Re-verify after build
01
technical approach

Unexpected Token < in JSON How to analyze?

The first character being '<' usually indicates that the expected JSON response is HTML. This could be due to a wrong endpoint, a 404/500 error page, a login redirect, a PHP warning, a WAF challenge, or a WISECP theme output being attempted to be parsed like JSON by response.json().

01

Find the first mistake

A SyntaxError may stop all subsequent scripts; First resolve the first red record in the Console.

02

Open the actual file

Determine whether the error is source or build with Source map, pretty print and Network URL.

03

Isolate with parser

Reduce the issue to a small area with node --check, ESLint, TypeScript or JSON validator.

04

Verify live output

Check the PHP render, API body, CDN cache and module loader result in the production answer.

Do not hide JSON errors using try/catch and use the HTML response as normal data.

02
Live error dictionary

Console and terminal messages

01kritik

Unexpected token '<'

Meaning: Response starts with HTML instead of JSON.

Possible cause: 404, 500, login, or theme page.

02kritik

<!DOCTYPE html> is not valid JSON

Meaning: Full HTML document is sent to JSON parser.

Possible cause: Incorrect endpoint or redirection.

03warning

Unexpected token P in JSON

Meaning: PHP warning/parse error text may get mixed up with JSON.

Possible cause: display_errors is open or fatal error.

04warning

Unexpected token A in JSON

Meaning: Access denied text response was returned.

Possible cause: Authority, session or CSRF issue.

05warning

Content-Type text/html

Meaning: The server is sending HTML content type instead of JSON.

Possible cause: Route/handler wrong.

06warning

302 login redirect

Meaning: API call was redirected to the login page.

Possible cause: Session duration or auth middleware.

07warning

Cloudflare challenge HTML

Meaning: WAF bot validation page returned.

Possible cause: Rate limit or security rule.

08bilgi

Empty response shown as HTML error

Meaning: Proxy generates custom error page.

Possible cause: Nginx/Apache upstream error.

No records matching this expression were found.

03
Copiable controls

Node.js, API, PHP and file tests

Header and status code

curl -i https://example.com/api/data

HTTP status, Content-Type and response are shown together.

Redirect Chain

curl -sIL https://example.com/api/data | grep -iE '^HTTP|^location:|^content-type:'

This shows redirecting to the login or error page.

JSON validation

curl -s https://example.com/api/data | python3 -m json.tool

Response is not valid JSON, gives parsing error.

First 300 Characters

curl -sL https://example.com/api/data | head -c 300

Response starts with HTML, PHP error message or JSON.

PHP Log Check

tail -n 100 /var/log/php-fpm/error.log 2>/dev/null || tail -n 100 /var/log/php8.3-fpm.log

Displays PHP errors that occurred during the API call.

Fetch and debug securely.

fetch('/api/data').then(async r => ({status:r.status,type:r.headers.get('content-type'),url:r.url,body:await r.text()})).then(console.log)

Before parsing, show status, content type, final URL, and body.

04
Correct and incorrect code

Syntax comparisons

Direct JSON parse

incorrect
const data = await response.json();
True
const metin = await response.text();
if (!response.ok) throw new Error(`${response.status}: ${metin.slice(0,200)}`);
const veri = JSON.parse(metin);

PHP header eksik

incorrect
echo json_encode($veri);
True
header('Content-Type: application/json; charset=utf-8');
echo json_encode($veri, JSON_UNESCAPED_UNICODE | JSON_THROW_ON_ERROR);

HTML error response

incorrect
http_response_code(500);
echo '<h1>Error</h1>';
True
http_response_code(500);
header('Content-Type: application/json; charset=utf-8');
echo json_encode(['success'=>false,'error'=>'Sunucu errorsı']);

Session cookie eksik

incorrect
fetch('/api/profile')
True
fetch('/api/profile', { credentials: 'same-origin', headers: { Accept: 'application/json' } })
05
According to working environment

Browser, Node.js, PHP and WISECP

Browser and Frontend

Chrome DevTools Console, Sources, and Network panels should be examined together with real file, line, and server response.

  • Find the actual source line with pretty print and source map.
  • Check if the response is returned as HTML instead of JavaScript/JSON using Network Response.
  • Verify script type, load order, defer, and module settings.

Node.js, TypeScript, and Packages

Node version, package.json module type, build output, and the actual file run should comply with the same module system.

  • Isolate the parser error with node --check.
  • Evaluate package.json, tsconfig, and file extensions together.
  • Verify if the build output is executed or not instead of the source.

PHP, AJAX and WISECP

PHP warnings, theme HTML, redirects, and double script loading JavaScript errors may appear.

  • Verify API response does not contain PHP warning or HTML mix.
  • Manage json_encode and Content-Type output.
  • Do not load the same JavaScript file twice within the header/footer.
Incorrect interventions

Absolutely don't

  • Do not hide JSON errors using try/catch and use the HTML response as normal data.
  • Do not output display_errors in the Production API response body.
  • Do not send Content-Type application/json and produce HTML in the body.
  • Do not temporarily bypass WAF or auth issues by changing the endpoint name.
Post-procedure check

Verify the solution

  • API returns correct HTTP status and application/json Content-Type.
  • Response is parsed with python -m json.tool.
  • Login and error conditions are using a consistent JSON contract.
  • Frontend parses after response.ok and content type check.
06
Internal SEO content set

Related JavaScript error solutions

07
primary sources

MDN, Node.js and official documentation

08
Frequently asked questions

Unexpected Token < in JSON Curiosities about

Unexpected Token < in JSON why it occurs?

It occurs when the JavaScript parser, JSON parser, or module loader cannot find the expected grammar.

Is the line in the console always correct?

The line is usually where the parser detects the error. Missing quotes, parentheses, or commas may be in the previous line.

How to find an error in a minified file?

Use source map and DevTools pretty print; correct the original source file and rebuild.

Why does PHP produce a JavaScript SyntaxError?

PHP warning, HTML error page or unpadded data pressed may break JavaScript/JSON syntax.

Does ESLint find all SyntaxError errors?

Most parsers find most errors; however, incorrect server response, double script loading, and runtime module configuration should also be checked.

Code was working, why did it suddenly fail?

Deploy, minify, cache, package update, Node version, PHP output or half-loaded file behavior may have changed.

Does this guide provide a secure production fix?

Provides diagnostic and safe repair steps; changes should be tested in the staging environment and reverted using Git.

EKA SOFTWARE AND INFORMATION SYSTEMS

Let's analyze JavaScript, Node.js and API error with source code

We examine Console, Fetch/AJAX, PHP JSON responses, ES Modules, TypeScript build and WISECP script loading problems without disrupting the production structure.

Get Software SupportWhatsApp
Top