How to Resolve `"Redirect has been blocked by CORS policy: No Access-Control-Allow-Origin' header" ? | A1Lab@FahadM to JavaScriptEnglish • 4 days ago

How to Resolve `"Redirect has been blocked by CORS policy: No Access-Control-Allow-Origin' header" ?

message-square
0

The browser error Redirect has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header occurs when a cross-origin request (fetch(), XMLHttpRequest, or $.ajax()) receives an HTTP redirect (301, 302, 307, 308)—often from http:// to https://, an auth login page, or a trailing-slash/host redirect—and the redirect target (or intermediate response) lacks a matching Access-Control-Allow-Origin response header. To fix it, call the final redirect destination URL directly, configure the redirect target server to send Access-Control-Allow-Origin for your frontend origin, or handle cross-origin requests/redirects through a same-origin backend proxy.


1. Point Requests to the Final URL and Configure CORS Headers on the Target Server

Check the Network tab in browser DevTools to inspect the 30x redirect Location header, then target the final HTTPS/canonical URL directly or return Access-Control-Allow-Origin on the redirected response:

// Example Express middleware sending CORS headers on responses (including redirected targets)
const express = require("express");
const app = express();

const ALLOWED_ORIGINS = new Set([
  "http://localhost:3000",
  "https://app.example.com"
]);

app.use((req, res, next) => {
  const origin = req.headers.origin;
  if (origin && ALLOWED_ORIGINS.has(origin)) {
    res.setHeader("Access-Control-Allow-Origin", origin);
    res.setHeader("Vary", "Origin");
    res.setHeader("Access-Control-Allow-Methods", "GET, POST, PUT, PATCH, DELETE, OPTIONS");
    res.setHeader("Access-Control-Allow-Headers", "Content-Type, Authorization");
  }

  if (req.method === "OPTIONS") {
    return res.sendStatus(204);
  }

  next();
});

app.get("/api/data", (req, res) => {
  res.json({ ok: true, data: [1, 2, 3] });
});
  • Use https:// and exact origins: Calling http:// endpoints that redirect to https://—or hitting a bare domain that redirects to www.—frequently triggers this error.
  • Avoid redirecting unauthenticated API requests to HTML login pages: If an API redirects an unauthenticated fetch() call to /login on another origin or without CORS headers, the browser blocks the redirect.

2. Use a Same-Origin Backend Proxy When You Do Not Control the Target Server

When a third-party or external API redirects cross-origin and you cannot modify its CORS headers, proxy the request through your own backend server (such as Node/Express or a dev-server proxy) so the browser only talks to your own origin:

// Express backend route proxying a cross-origin request server-to-server
app.get("/api/proxy/resource", async (req, res) => {
  try {
    const upstreamResponse = await fetch("https://external-api.example.com/resource", {
      redirect: "follow"
    });

    const bodyText = await upstreamResponse.text();
    res.status(upstreamResponse.status);

    const contentType = upstreamResponse.headers.get("content-type");
    if (contentType) {
      res.setHeader("Content-Type", contentType);
    }

    res.send(bodyText);
  } catch (err) {
    res.status(502).json({ error: "Upstream request failed", detail: err.message });
  }
});
  • Server-to-server requests do not enforce browser CORS: Your backend fetches the upstream URL and returns the result to your frontend over a same-origin route.
  • Keep secrets on the server: Proxying also keeps API keys or tokens out of browser client code.

3. Common Traps to Avoid

Fixing CORS redirect errors often stalls on a few common missteps:

  • Trying to bypass CORS inside client-side fetch(): Client options like mode: "no-cors" do not let readable cross-origin JSON responses bypass CORS—only server headers (Access-Control-Allow-Origin) or a backend proxy solve it.
  • Using wildcard * when credentials (Authorization / cookies) are sent: Browsers reject Access-Control-Allow-Origin: * when requests use credentials—echo the specific allowed origin instead.
  • Overlooking trailing-slash or http -> https hops: Even a single 301 redirect to a different host or scheme requires CORS compliance on the response chain.

TL;DR: Inspect the 30x Location header in DevTools Network, call the final target URL directly, ensure the target server returns Access-Control-Allow-Origin, or route the call through a same-origin backend proxy.

Are you seeing this on an http -> https or www redirect, an auth redirect to a login page, or a third-party API you do not control?