Problem
Your client-side web application fails to fetch API records and displays a console exception:
Access to fetch at 'https://api.your-domain.com' from origin 'https://your-domain.com' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.
Symptoms
- Outgoing HTTP API fetch connections fail with status
(failed) net::ERR_FAILED. - Server log logs show successful
OPTIONSpreflight requests, but subsequent fetches crash. - Web client parameters cannot retrieve data scopes from server payloads.
Root Cause
CORS (Cross-Origin Resource Sharing) is a browser security guardrail that prevents web pages from making requests to a different domain than the one that served the web page, unless the target server explicitly grants permission.
Mismatches occur when:
- Missing Server Headers: The backend router does not return the
Access-Control-Allow-Originheader in HTTP responses. - Preflight Failure: The browser sends a preflight
OPTIONSrequest before the actual request, and the server fails to handle this request with a200 OKstatus and the appropriate headers. - Credentials Mismatch: The client requests credentials (
credentials: 'include'), but the server allowsAccess-Control-Allow-Origin: *(wildcards are not permitted with credentials).
Quick Fix
Install and configure standard CORS middleware headers on your backend.
Node.js / Express Example:
// ❌ FAILS CORS
const express = require('express');
const app = express();
app.get('/api/data', (req, res) => {
res.json({ data: "Locked resource" });
});
// ✔ SOLVES CORS
const express = require('express');
const cors = require('cors');
const app = express();
// Configure strict allowed origins
app.use(cors({
origin: 'https://your-client-domain.com',
methods: ['GET', 'POST', 'PUT', 'DELETE'],
credentials: true
}));
app.get('/api/data', (req, res) => {
res.json({ data: "Accessible resource" });
});
Prevention
- Avoid utilizing wildcard origins (
*) in production code contexts. - Handle
OPTIONSmethods globally at reverse proxy layers (like Nginx, Cloudflare) if possible. - Verify allowed headers are passed during preflight requests.