Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Next.js and React

response.json() fails after fetch with mode: no-cors

ErrorSyntaxError: Unexpected end of input

Short answer

no-cors does not bypass CORS; it hides the response. The result is opaque with status 0 and a null body, so json() has nothing to parse and rejects with a SyntaxError. Remove mode: no-cors and make the API send Access-Control-Allow-Origin, or call the API from your own server.

  • Applies to React 19
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Log type, status and body of the response before you call json(). An opaque response with status 0 means no-cors is hiding the data.

    const response = await fetch(url, { mode: "no-cors" });
    console.log(response.type, response.status, response.body);
  2. 2Match what you see, then jump to its fix

    1. 1type is opaque, status 0, body nullmode: no-cors returns an opaque responseFix
    2. 2After removing no-cors, the console shows a CORS error insteadThe API does not send Access-Control-Allow-OriginFix
    3. 3An OPTIONS request fails before the real callA JSON request sets headers that trigger a preflightFix
    4. 4status 200 or 204 and the body text is emptyThe server really sent an empty or non-JSON bodyFix

What does the error look like?

  • SyntaxError: Unexpected end of input
  • response.type is "opaque" and response.status is 0

Cause 1 of 4

mode: no-cors returns an opaque response

1Check

In no-cors mode a cross-origin response is opaque: JavaScript cannot read its headers or body. Parsing an empty body as JSON rejects with a SyntaxError.

console.log(response.type);   // "opaque"

2Fix

Remove mode: no-cors. Without it, your code can read the response if the server allows it (next cause).

const response = await fetch("https://api.example.com/items");
const data = await response.json();

3Verify

response.type is no longer opaque and status is 200 or the real error code.

console.log(response.type, response.status);

Confirmed by developer.mozilla.org

Did this fix it?

Cause 2 of 4

The API does not send Access-Control-Allow-Origin

1Check

A web page can read a cross-origin response only when the server opts in with Access-Control-Allow-Origin. Check the response headers with curl, sending your page's origin.

curl -i -H "Origin: http://localhost:5173" https://api.example.com/items

2Fix

Add the header on the API for your origin. If you do not control the API, call it from your own backend and let your frontend call that backend on the same origin.

Access-Control-Allow-Origin: http://localhost:5173
Vary: Origin

3Verify

curl shows the header and the browser call succeeds.

curl -i -H "Origin: http://localhost:5173" https://api.example.com/items | grep -i access-control

Confirmed by developer.mozilla.org

Did this fix it?

Cause 3 of 4

A JSON request sets headers that trigger a preflight

1Check

A request with a method other than GET, HEAD or POST, a non-safelisted header, or a content type outside the simple list is preceded by an OPTIONS preflight. The server must answer it with the allowed methods and headers.

curl -i -X OPTIONS https://api.example.com/items \
  -H "Origin: http://localhost:5173" \
  -H "Access-Control-Request-Method: POST" \
  -H "Access-Control-Request-Headers: content-type"

2Fix

Have the server return Access-Control-Allow-Methods and Access-Control-Allow-Headers for the preflight.

Access-Control-Allow-Origin: http://localhost:5173
Access-Control-Allow-Methods: POST, GET, OPTIONS
Access-Control-Allow-Headers: Content-Type

3Verify

The OPTIONS response is 2xx with those headers.

// repeat the curl and read the headers

Confirmed by developer.mozilla.org

Did this fix it?

Cause 4 of 4

The server really sent an empty or non-JSON body

1Check

json() rejects with a SyntaxError when the body cannot be parsed as JSON, and an empty body cannot. Read it as text first.

const text = await response.text();
console.log(JSON.stringify(text));

2Fix

Parse only when there is a body, and fix the API if it should have returned JSON.

const text = await response.text();
const data = text ? JSON.parse(text) : null;

3Verify

No SyntaxError; data is null for an empty body.

console.log(data);

Confirmed by developer.mozilla.org

Did this fix it?

How do I stop it coming back?

  • Never use mode: no-cors to read data; it is only for requests whose answer you do not need.
  • Use a dev proxy or your own backend for third-party APIs without CORS.
  • Check response.ok and read text before json() in shared fetch helpers.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.