Fix Cross-Origin Resource Sharing (CORS) errors

CORS errors occur when your frontend makes requests to a different origin (domain, port, or protocol) and the server doesn't include the correct Access-Control-Allow-Origin headers. The browser blocks the response to protect users.

Error messages you might see

Access to fetch at 'https://api.example.com' from origin 'http://localhost:3000' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.
CORS policy: Response to preflight request doesn't pass access control check: The 'Access-Control-Allow-Origin' header has a value that is not equal to the supplied origin.

Why this happens in AI-generated code

1

API server not configured for cross-origin requests

AI-generated backend code often doesn't include CORS middleware or headers, because the AI tested with same-origin requests or server-side calls where CORS doesn't apply.

2

Preflight OPTIONS request not handled

AI tools generate API endpoints that handle POST/PUT but miss the OPTIONS method. Browsers send a preflight OPTIONS request for non-simple requests, and without a response, the actual request is blocked.

3

Credentials mode mismatch

AI-generated code includes credentials: 'include' in fetch but the server responds with Access-Control-Allow-Origin: * which is incompatible - credentials require a specific origin.

How to fix it

1

Configure CORS headers on your API

Add Access-Control-Allow-Origin with your frontend's origin, handle OPTIONS preflight requests, and include Access-Control-Allow-Methods and Access-Control-Allow-Headers.

2

Use Next.js API routes as a proxy

Instead of calling external APIs directly from the browser, proxy requests through Next.js API routes (same origin) to avoid CORS entirely.

3

Get professional help

Still stuck? Our engineers can configure CORS correctly across your stack. Visit /products to get started.

Related technologies

Can't fix it yourself?

Our code audit identifies this issue and dozens more. Get a prioritized fix list.

Security Review

Automated Security Scan

$19

AI-powered analysis of your codebase. Get a detailed report with prioritized findings within 24 hours.

Get Started
Most Popular

Security Review

Manual Security Review

from $250

Expert engineer works on your project directly. Fixed scope, fixed price, no surprises.

Get a Quote

Security Review

Full Pentest

Custom

Enterprise-grade engagement tailored to your needs. Dedicated engineer, ongoing support.

Fix Bugs

Code Audit

$19

AI-powered analysis of your codebase. Get a detailed report with prioritized findings within 24 hours.

Get Started
Most Popular

Fix Bugs

Bug Fixing

from $200

Expert engineer works on your project directly. Fixed scope, fixed price, no surprises.

Get a Quote

Fix Bugs

Ongoing Support

Custom

Enterprise-grade engagement tailored to your needs. Dedicated engineer, ongoing support.

Refactor Code

Code Audit

$19

AI-powered analysis of your codebase. Get a detailed report with prioritized findings within 24 hours.

Get Started
Most Popular

Refactor Code

Refactoring

from $400

Expert engineer works on your project directly. Fixed scope, fixed price, no surprises.

Get a Quote

Refactor Code

Full Rewrite

Custom

Enterprise-grade engagement tailored to your needs. Dedicated engineer, ongoing support.

100% of your audit purchase is credited toward any paid service. Start with an audit, then let us fix what we find.

Frequently asked questions

Why does my API work in Postman but not in the browser?

CORS is enforced only by browsers. Postman, curl, and server-side code don't enforce CORS because they aren't protecting a user browsing a different website. The API works - it's the browser blocking the response.

Is it safe to set Access-Control-Allow-Origin to *?

For public APIs with no authentication, wildcard (*) is fine. For APIs that use cookies or Authorization headers, you must specify the exact allowed origin - wildcards are not permitted with credentials.

Still stuck? We can fix it for you.

Send us your repo. We'll diagnose the issue and give you a fixed quote within 24 hours.

Tell Us About Your App