Skip to content
Browser messages3 min read

CORS error: "has been blocked by CORS policy" between frontend and API

Access to fetch at 'https://api.example.com/...' from origin 'https://example.com' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.

Short answer

"Blocked by CORS policy" means the browser requested data from a different origin than the page, and the API did not answer with an Access-Control-Allow-Origin header allowing that origin, so the browser withheld the response. After a deploy the cause is usually a frontend still calling localhost, CORS settings that only allow localhost, or an API answering with a 500 that carries no CORS headers.

ArabyHost teamLast updated: ShareWhatsAppXLinkedIn
Contents
  1. Read the end of the message
  2. Causes, most likely first
  3. How to confirm it is fixed
  4. How to avoid it
  5. On ArabyHost

The page loads, but no data appears, login fails, and the Console has a long red line containing blocked by CORS policy. CORS (Cross-Origin Resource Sharing) is a browser security rule: a page on https://example.com may only read a response from https://api.example.com if the API explicitly says it allows that origin. So the problem is the API’s settings or the URL the frontend calls, not the browser.

Read the end of the message

The message ends with Meaning
No 'Access-Control-Allow-Origin' header is present The API sent no allow header, or failed before sending it
Response to preflight request doesn't pass access control check The preflight OPTIONS request failed or returned non-2xx
Redirect is not allowed for a preflight request The API redirected the request (http to https, or a trailing slash)
must not be the wildcard '*' when the request's credentials mode is 'include' You send cookies and the API allows *
The 'Access-Control-Allow-Origin' header contains multiple values The header is added twice, for example by the code and by .htaccess

Open Network and click the failed request: the real status code (404 or 500) is shown there.

Causes, most likely first

1. The frontend still calls localhost

Code written by an AI tool often contains http://localhost:5000/api. On a visitor’s device there is nothing at localhost, so you get a CORS error or ERR_CONNECTION_REFUSED. Search the code for localhost, make the URL an environment variable such as VITE_API_URL=https://api.example.com, and rebuild. Frontend variables are baked in at build time, as explained in .env not loaded.

2. The API’s CORS settings only allow localhost

Express (Node.js):

const cors = require('cors');
app.use(cors({
  origin: ['https://example.com', 'https://www.example.com'],
  credentials: true,
}));

Put it before your routes, then restart the app in Setup Node.js App with Restart.

Laravel: in Laravel 11, create the config file with php artisan config:publish cors, then edit config/cors.php:

'paths' => ['api/*', 'sanctum/csrf-cookie'],
'allowed_origins' => ['https://example.com', 'https://www.example.com'],
'supports_credentials' => true,

Then php artisan config:clear. A URL outside paths gets no CORS headers.

Plain PHP: at the top of the API entry file:

header('Access-Control-Allow-Origin: https://example.com');
header('Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS');
header('Access-Control-Allow-Headers: Content-Type, Authorization');
if ($_SERVER['REQUEST_METHOD'] === 'OPTIONS') { http_response_code(204); exit; }

3. The API fails, and the failure looks like CORS

When the API crashes with a 500 error or cannot find the route, it usually answers without CORS headers, so the browser shows a CORS message and hides the real cause. Check the status code in Network and read the error log. For Node apps on Passenger, see Passenger app error.

4. The OPTIONS preflight or a redirect fails

Requests that send Authorization or Content-Type: application/json are preceded by an OPTIONS request. If the API answers it with 404, 405 or a redirect, the whole call fails. Use the API’s final URL exactly (https, with or without www as the server uses it), and make sure your CORS library handles OPTIONS.

5. www versus non-www, or http versus https

https://example.com, https://www.example.com and http://example.com are three origins. Allow every form your visitors use, or redirect them all to one.

How to confirm it is fixed

In Network, click the request: under Response Headers you should see access-control-allow-origin: https://example.com, and the OPTIONS request should return 200 or 204. Then test login in a private window.

How to avoid it

  • Serve the frontend and API from the same origin where you can, as in the full-stack deployment guide.
  • Keep the API URL and allowed origins in environment variables, not hard-coded.

On ArabyHost

Express and FastAPI apps run through Setup Node.js App and Setup Python App on AI/Developer plans, Laravel and PHP on every plan, and you can run the frontend on the domain and the API on a subdomain of the same account. Free SSL is issued automatically for both, so keep every URL https. If the root cause is an API error, the Araby AI assistant reads the error log and explains the fix. Deployment details are in the Node.js API guide.

FAQ

Why does it work in Postman or curl but fail in the browser?

CORS is enforced only by browsers, to protect users. Postman and curl ignore it, so their success means the API works and the missing piece is the header that allows your domain.

Can I just set Access-Control-Allow-Origin: * and be done?

For a public API without login, yes. If you send cookies or credentials, browsers reject the wildcard and you must name the exact origin, such as https://example.com.

Are example.com and www.example.com the same origin?

No. Any difference in scheme, subdomain or port makes a different origin. Allow both, or redirect one to the other.

Can I avoid CORS entirely?

Yes, by serving the frontend and API from the same origin, such as example.com for the site and example.com/api for the API. The request is then not cross-origin.

Useful? Share it with someone who needs it:WhatsAppXLinkedIn

Read next

Ready to put your idea online?

Fast, isolated hosting, support that understands you, and an AI assistant that fixes problems with you.