Skip to content
FunnelGuard

Fix guide by FunnelsGuard

CORS error blocking an API call from your page: how to fix it

PRF-124High impactDetectedBy FunnelGuardPublished Updated
On this page

What this issue is

CORS stands for Cross-Origin Resource Sharing. An origin is the combination of protocol, domain and port, such as https://www.example.com. When JavaScript on your page calls an API on a different origin, the browser asks that server for permission. The server gives permission with response headers such as Access-Control-Allow-Origin.

If the permission is missing or wrong, the browser blocks your code from using the response and logs an error in the console, for example: Access to fetch at 'https://api.example.com/lead' from origin 'https://www.example.com' has been blocked by CORS policy. The request may still reach the server. Your page just cannot use the answer.

Why it costs you leads or sales

Many funnel steps depend on a script calling an API: sending a lead form to a CRM, checking a postcode, loading shipping rates, adding to a cart, creating a payment session or booking a slot. If CORS blocks that call, the step fails. Visitors usually see a spinner that never ends, a button that does nothing, or a generic error. Most of them leave and you never hear about it.

It also tends to start without warning. A developer changes an API domain, a CDN setting, a redirect or a security plugin, and a flow that worked yesterday breaks for every visitor.

Where a CORS error breaks the funnel
  1. Ad clickVisitor lands on the page
  2. Fills in formDetails entered, button clicked
  3. API callBlocked by CORS policy in the browser
  4. Thank-you pageNever reached
  5. Conversion tagNever fires

The visitor does everything right, but the browser blocks the response the page needs.

Form submissions before and after a CORS breakExample numbers
Form submissions before and after a CORS break (example)Before the break: 40 submissions per day; After the break: 2 submissions per day01531402Before the breakAfter the breaksubmissions per day

Example: illustrative numbers for a form that posts to an API on another domain. Traffic stays the same, successful submissions drop to near zero.

How to check if your site has it

Find a CORS error in your browser
  1. Open DevTools on the broken step

    In Chrome, Edge or Firefox press F12 (or Cmd+Option+I on Mac) and open the Console tab. Reload the page.

  2. Repeat the action

    Submit the form, add to cart or click the button that calls the API.

  3. Look for the CORS message

    Typical wording: 'blocked by CORS policy', 'No Access-Control-Allow-Origin header is present' or 'Response to preflight request doesn't pass access control check'.

  4. Inspect the request in the Network tab

    Filter by Fetch/XHR. Click the failed request and read the Request URL, status code and Response Headers. Check whether an OPTIONS (preflight) request came first and what it returned.

  5. Test from a clean session

    Use a private window with extensions off. Ad blockers can also produce errors that look similar but are a different problem.

Common CORS messages and what they usually mean
Console messageLikely cause
No 'Access-Control-Allow-Origin' header is presentThe API does not send the header at all, or an error page or redirect replaced the response
The 'Access-Control-Allow-Origin' header has a value that is not equal to the supplied originYour site's origin is not on the allowed list (www vs non-www, http vs https, wrong port)
Response to preflight request doesn't pass access control checkThe OPTIONS request returned an error, a redirect, or lacked allowed methods or headers
Request header field ... is not allowed by Access-Control-Allow-HeadersYour code sends a header such as Authorization or Content-Type that the API has not allowed
Wildcard '*' cannot be used when credentials mode is 'include'The API uses * while your request sends cookies; it must name the exact origin

How to fix it

The fix almost always belongs on the server that receives the call, not in your page. You can't solve it by changing front-end code alone. If you don't control that server (a third-party API), call it from your own backend or use the vendor's supported endpoint instead.

Fix a CORS error step by step
  1. Confirm the exact origin

    Copy the origin from the console message. Note www vs non-www, http vs https and any port. The allowed value must match it exactly.

  2. Allow that origin on the API

    Send Access-Control-Allow-Origin with your site's origin. Prefer a specific allowlist over * for anything that uses cookies or private data.

  3. Answer the preflight request

    For JSON POSTs, custom headers or methods like PUT and DELETE, the browser sends an OPTIONS request first. Return 204 or 200 with Access-Control-Allow-Methods and Access-Control-Allow-Headers, and no redirect.

  4. Send the headers on errors and redirects too

    A 4xx or 5xx response without the header looks like a CORS error and hides the real problem. Make sure error responses carry the header.

  5. Avoid redirects on the API URL

    Call the final URL directly (https, correct host, trailing slash as required). Preflight requests cannot follow redirects.

  6. Or remove the cross-origin call

    Serve the API from the same origin (for example a path like /api on your domain) or proxy it through your server, so no CORS check applies.

Where to set the headers on common platforms
  1. Find who sends the request

    If your own REST route is called from another domain, use the rest_pre_serve_request filter or the rest_allowed_cors_headers filter in a small plugin, not the theme, and allow only your origin.

  2. Check plugins and the host

    Security plugins, caching plugins and some hosts add or strip headers. Check their settings, and purge the cache after changing anything.

nginx: allow one origin and answer preflightnginx
location /api/ {
    add_header Access-Control-Allow-Origin "https://www.example.com" always;
    add_header Vary Origin always;

    if ($request_method = OPTIONS) {
        add_header Access-Control-Allow-Origin "https://www.example.com" always;
        add_header Access-Control-Allow-Methods "GET, POST, OPTIONS" always;
        add_header Access-Control-Allow-Headers "Content-Type, Authorization" always;
        add_header Access-Control-Max-Age 86400 always;
        return 204;
    }

    proxy_pass http://app_backend;
}
Express: allow a list of originsjavascript
const express = require('express');
const cors = require('cors');
const app = express();

const allowed = ['https://www.example.com', 'https://example.com'];

app.use(cors({
  origin(origin, cb) {
    if (!origin || allowed.includes(origin)) return cb(null, true);
    cb(new Error('Not allowed by CORS'));
  },
  methods: ['GET', 'POST', 'OPTIONS'],
  allowedHeaders: ['Content-Type', 'Authorization']
}));

app.post('/api/lead', (req, res) => res.json({ ok: true }));
What changes when CORS is fixed

Blocked

  • Console shows 'blocked by CORS policy'
  • OPTIONS request fails or redirects
  • Form shows a spinner or nothing happens
  • No thank-you page, no conversion tag

Allowed

  • Response includes Access-Control-Allow-Origin for your origin
  • OPTIONS returns 204 with allowed methods and headers
  • Form completes and shows a confirmation
  • Thank-you page loads and tags fire

How to confirm it's fixed

Confirm the fix
  • Hard-reload the page in a private window and repeat the action that failed
  • The console shows no 'blocked by CORS policy' message
  • In the Network tab the OPTIONS request returns 200 or 204 and the real request returns the expected status
  • The response headers include Access-Control-Allow-Origin with your exact origin
  • Test both the www and non-www versions and any staging domain you use
  • Test on mobile and in a second browser such as Safari or Firefox
  • The thank-you page loads and conversion tags fire
  • A test lead or order arrives in your inbox or CRM

How to prevent it happening again

Prevention habits
  • Keep the list of allowed origins in one place and update it when you add a domain, subdomain or staging site
  • Retest forms and checkouts after any change to an API, CDN, proxy, security plugin or domain setup
  • Avoid redirects on API URLs, including http to https and trailing-slash redirects
  • Prefer same-origin or server-side calls for critical steps such as lead capture
  • Watch the console on key funnel steps after each release
  • Run a scheduled test of every key journey so a break is caught within hours

How FunnelGuard handles this

FunnelGuard detects this today. Its robot visitor runs your real journeys on a schedule, such as a lead form or checkout, and records console errors on each step. A CORS error shows up as a console error, so a blocked API call is flagged on the step where it happened.

A failure is re-checked from a second region before one alert goes out, by email, Slack, Teams, WhatsApp, webhook or PagerDuty. The alert includes a screenshot and a likely cause. FunnelGuard reports the error it sees; you still need to change the headers on the server that owns the API.

You can start with the Free plan (1 site, 2 journeys, daily checks, no card), or try a paid plan free for 3 days. A free read-only website audit is also available.

Questions people ask

Why is my form not submitting and the console says blocked by CORS policy?

Your page is sending the form data to an API on another origin, and that server did not allow your site in its Access-Control-Allow-Origin header. The browser blocks your script from using the response. Add your exact site origin to the API's allowed origins and make sure it answers the OPTIONS preflight request.

How do I fix 'No Access-Control-Allow-Origin header is present on the requested resource'?

Make the server that receives the request send an Access-Control-Allow-Origin header that matches your site's origin. Also check that the response is not an error page or redirect without the header. If you do not control the server, call it from your own backend instead.

Can I fix a CORS error from the front end only?

No. The browser enforces CORS using headers set by the server, so the fix must be made there. On the front end you can only avoid the problem, for example by calling a same-origin endpoint or a proxy on your own server.

Why does the API work in Postman but fail in the browser?

Postman and curl do not enforce CORS, but browsers do. The API may work for direct requests while still missing the headers browsers need. Check the Network tab for the OPTIONS request and the response headers.

What is a CORS preflight request?

A preflight is an OPTIONS request the browser sends before certain cross-origin calls, such as JSON POSTs or requests with an Authorization header. The server must reply with success and the allowed methods and headers. If it errors or redirects, the real request never runs.

Is using Access-Control-Allow-Origin: * safe?

It is acceptable for public data that needs no cookies or login. It does not work with credentialed requests, and it is risky for private APIs. Use an explicit allowlist of your own origins for anything sensitive.

Why does CORS fail on www but work on the non-www domain?

Origins must match exactly, and www.example.com and example.com are different origins. If only one is in the allowed list, the other is blocked. Add both, or redirect visitors to a single canonical domain.

How can I know when a CORS error starts affecting my funnel?

Run your key journeys on a schedule and watch for console errors on each step. FunnelGuard does this with a robot visitor and alerts you with a screenshot and a likely cause, after re-checking the failure from a second region.

Does FunnelGuard catch this?

Detected

FunnelGuard checks for this today.

How: console error.

Stop finding out from your customers

FunnelGuard runs your real forms and checkouts on a schedule, checks the tracking and the leads behind them, and alerts you once, with a screenshot. Try any paid plan free for 3 days.