response.json() fails after fetch with mode: no-cors
SyntaxError: Unexpected end of inputShort 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.
Fix it fast
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);2Match what you see, then jump to its fix
- 1
type is opaque, status 0, body nullmode: no-cors returns an opaque responseFix - 2
After removing no-cors, the console shows a CORS error insteadThe API does not send Access-Control-Allow-OriginFix - 3
An OPTIONS request fails before the real callA JSON request sets headers that trigger a preflightFix - 4
status 200 or 204 and the body text is emptyThe server really sent an empty or non-JSON bodyFix
- 1
What does the error look like?
SyntaxError: Unexpected end of inputresponse.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
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/items2Fix
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: Origin3Verify
curl shows the header and the browser call succeeds.
curl -i -H "Origin: http://localhost:5173" https://api.example.com/items | grep -i access-controlConfirmed by developer.mozilla.org
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-Type3Verify
The OPTIONS response is 2xx with those headers.
// repeat the curl and read the headersConfirmed by developer.mozilla.org
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
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:
- TaxiDexMy own multi-tenant taxi dispatch SaaS: API, dispatcher portal, driver and passenger apps, and the servers underneath.
- QistLockA NestJS and React SaaS driving an Android Device Owner agent that locks financed phones when payments lapse.
- WorkforceA field app, an admin console and a self-hosted Node.js server for push and reconciliation.
Related fixes
Web apps
Axios sends no cookie to an Express API even with withCredentials set to true
The login response has a Set-Cookie header, but the browser stores nothing
withCredentials only tells the browser to attach cookies.
4 causes, each with a fixWeb apps
An RTK Query request is blocked by CORS in the browser
The same request works in Postman or curl
CORS is enforced by the browser, so the API must send the right headers.
3 causes, each with a fixWeb apps
fetch() throws TypeError: Failed to fetch in the browser
Uncaught (in promise) TypeError: Failed to fetch
Failed to fetch means the request never produced a response the browser would hand to your code.
4 causes, each with a fixWeb apps
Socket.IO client fails to connect with xhr poll error
connect_error: xhr poll error
xhr poll error is only the reason string on the client's connect_error event: the first HTTP request to the server failed.
4 causes, each with a fixWeb apps
Module not found for antd/dist/antd.less after upgrading to Ant Design 5
Module not found: Error: Can't resolve 'antd/dist/antd.less'
Ant Design 5 dropped Less and the bundled CSS file for CSS-in-JS, so antd/dist/antd.less no longer exists.
3 causes, each with a fixWeb apps
Apollo Client is blocked by CORS when the GraphQL server runs on AWS Lambda
No 'Access-Control-Allow-Origin' header is present on the requested resource.
Apollo Server does not set CORS for you when it runs behind a framework or integration, and on Lambda that layer is API Gateway.
5 causes, each with a fix