Close Menu
Knowvenger | Your Daily Dose of TechKnowvenger | Your Daily Dose of Tech
  • Home
  • Cloud & DevOps
    • Networking & Security
    • AWS
  • Blockchain & Web3
    • Web3 Fundamentals
  • Web Development
    • HTTP & APIs
    • Frontend Development
  • System Design
    • Software Architecture
What's Hot

Understanding State Management in Angular: Strategies and Pitfalls

May 21, 2026

Understanding State Management in Angular: A Comprehensive Analysis

May 7, 2026

Evolution of React Server Components: Performance, Architecture, and User Experience

May 7, 2026
Facebook X (Twitter) Instagram
Knowvenger | Your Daily Dose of TechKnowvenger | Your Daily Dose of Tech
  • Home
  • Cloud & DevOps
    1. Networking & Security
    2. AWS
    3. View All

    Massive RSC Vulnerability Found in React 19 and Next.js | How to Protect Your App in 2025

    December 12, 2025

    Difference Between VPN and Proxy Server | Which One Should You Choose?

    November 2, 2025

    How to Create Your Own VPN on a Private Server

    October 26, 2025

    10 Best Practices for Optimizing Your AWS Resource Management to Reduce Costs

    April 17, 2026

    How to Reset MFA in AWS Cognito Hosted UI

    October 6, 2025

    10 Best Practices for Optimizing Your AWS Resource Management to Reduce Costs

    April 17, 2026

    Building Scalable Applications in the Cloud: Best Practices for AWS Deployment

    April 17, 2026

    AI in Cloud Infrastructure: Smarter Automation & Optimization

    March 11, 2026

    Shared vs VPS vs Cloud Hosting | Which Is Best ?

    January 15, 2026
  • Blockchain & Web3
    1. Web3 Fundamentals
    2. View All

    Web3 | How Decentralization Is Changing the Internet in 2025

    October 13, 2025

    Web3 | How Decentralization Is Changing the Internet in 2025

    October 13, 2025
  • Web Development
    1. HTTP & APIs
    2. Frontend Development
    3. View All

    Mastering HTTP 2.0: Enhancing Performance and Security for Modern Web Applications

    April 17, 2026

    10 Best Practices for Securing APIs in Cloud and DevOps Environments

    April 17, 2026

    10 Essential Best Practices for Securing Your APIs in 2026

    April 13, 2026

    10 Essential Best Practices for Building Secure APIs in a Cloud Environment

    March 14, 2026

    Understanding State Management in Angular: Strategies and Pitfalls

    May 21, 2026

    Understanding State Management in Angular: A Comprehensive Analysis

    May 7, 2026

    Evolution of React Server Components: Performance, Architecture, and User Experience

    May 7, 2026

    Impact of Angular’s Dependency Injection Updates

    May 3, 2026

    Understanding State Management in Angular: Strategies and Pitfalls

    May 21, 2026

    Understanding State Management in Angular: A Comprehensive Analysis

    May 7, 2026

    Evolution of React Server Components: Performance, Architecture, and User Experience

    May 7, 2026

    Impact of Angular’s Dependency Injection Updates

    May 3, 2026
  • System Design
    1. Software Architecture
    2. View All

    Microservices Architecture

    March 1, 2026

    Monolith Architecture

    January 2, 2026

    Microservices Architecture

    March 1, 2026

    Monolith Architecture

    January 2, 2026
Knowvenger | Your Daily Dose of TechKnowvenger | Your Daily Dose of Tech
Home » Understanding CORS (Cross-Origin Resource Sharing)
HTTP & APIs

Understanding CORS (Cross-Origin Resource Sharing)

yasiru_jayashanBy yasiru_jayashanOctober 27, 2025Updated:January 15, 2026No Comments2 Mins Read
Share Facebook Twitter Pinterest LinkedIn Tumblr Reddit Telegram Email
understanding-cors
Share
Facebook Twitter LinkedIn Pinterest Email

Introduction

If you’ve ever seen an error like “Access to fetch at ‘https://api.example.com’ from origin ‘https://yourapp.com’ has been blocked by CORS policy”, you’ve encountered CORS Cross-Origin Resource Sharing.

What is CORS?

CORS (Cross-Origin Resource Sharing) is a mechanism that allows a web application running on one domain (e.g., frontend.com) to request resources from another domain (e.g., api.server.com) in a secure way.

Without CORS, web browsers restrict such cross-origin requests by default this is part of the Same-Origin Policy, a core web security model.

How Does CORS Work?

When your frontend app tries to fetch data from another domain, the browser first sends an HTTP request with special headers like Origin and Access-Control-Request-Method.

The server then responds with CORS-specific headers that tell the browser whether the request is allowed.

Example:

Request (Browser → Server):

GET /data HTTP/1.1
Host: api.server.com
Origin: https://frontend.com

Response (Server → Browser):

HTTP/1.1 200 OK
Access-Control-Allow-Origin: https://frontend.com
Access-Control-Allow-Methods: GET, POST, OPTIONS
Access-Control-Allow-Headers: Content-Type

If the browser sees that the Access-Control-Allow-Origin matches your domain, it allows the response. Otherwise, it blocks it.

Common CORS Errors and How to Fix Them

1. “No ‘Access-Control-Allow-Origin’ header present”

Cause: The server didn’t send the required CORS header.

Fix: Add the Access-Control-Allow-Origin header to the server response.

Example in PHP:

header("Access-Control-Allow-Origin: *");

(Note: Use * only for public APIs. Use your exact domain for restricted ones.)

2. “CORS policy: Preflight request didn’t succeed”

Cause: The server didn’t handle the OPTIONS request properly.

Fix: Configure the server to respond to OPTIONS requests:

Example in PHP:

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

3. “Credential is not supported if the CORS header ‘Access-Control-Allow-Origin’ is ‘*’”

Cause: You’re sending cookies or authentication tokens.

Fix: Use a specific origin and enable credentials:

Access-Control-Allow-Origin: https://frontend.com
Access-Control-Allow-Credentials: true

Best Practices for CORS

  • Allow only trusted origins – Never use * for private APIs.
  • Use HTTPS – Avoid mixed-content issues.
  • Handle preflight requests properly – Especially for PUT, DELETE, and POST methods.
  • Use middleware for simplicity – Most frameworks (Laravel, Express, Django) offer built-in CORS support.
  • Test with browser dev tools – Use the Network tab to inspect request/response headers.

Share. Facebook Twitter Pinterest LinkedIn Tumblr Email
Previous ArticleHow to Create Your Own VPN on a Private Server
Next Article Difference Between VPN and Proxy Server | Which One Should You Choose?
yasiru_jayashan
  • Website

Related Posts

HTTP & APIs

Mastering HTTP 2.0: Enhancing Performance and Security for Modern Web Applications

April 17, 2026
HTTP & APIs

10 Best Practices for Securing APIs in Cloud and DevOps Environments

April 17, 2026
HTTP & APIs

10 Essential Best Practices for Securing Your APIs in 2026

April 13, 2026
Add A Comment
Leave A Reply Cancel Reply

Top Posts

Angular subscribe() Function Explained

October 18, 2025112 Views

How to Reset MFA in AWS Cognito Hosted UI

October 6, 202592 Views

Web3 | How Decentralization Is Changing the Internet in 2025

October 13, 202562 Views
Stay In Touch
  • Facebook
  • YouTube
  • TikTok
  • WhatsApp
  • Twitter
  • Instagram
Latest Reviews
Most Popular

Angular subscribe() Function Explained

October 18, 2025112 Views

How to Reset MFA in AWS Cognito Hosted UI

October 6, 202592 Views

Web3 | How Decentralization Is Changing the Internet in 2025

October 13, 202562 Views
Our Picks

Understanding State Management in Angular: Strategies and Pitfalls

May 21, 2026

Understanding State Management in Angular: A Comprehensive Analysis

May 7, 2026

Evolution of React Server Components: Performance, Architecture, and User Experience

May 7, 2026
© 2026 Knowvenger. All rights reserved.
  • Home
  • Cloud & DevOps
    1. Networking & Security
    2. AWS
    3. View All

    Massive RSC Vulnerability Found in React 19 and Next.js | How to Protect Your App in 2025

    December 12, 2025

    Difference Between VPN and Proxy Server | Which One Should You Choose?

    November 2, 2025

    How to Create Your Own VPN on a Private Server

    October 26, 2025

    10 Best Practices for Optimizing Your AWS Resource Management to Reduce Costs

    April 17, 2026

    How to Reset MFA in AWS Cognito Hosted UI

    October 6, 2025

    10 Best Practices for Optimizing Your AWS Resource Management to Reduce Costs

    April 17, 2026

    Building Scalable Applications in the Cloud: Best Practices for AWS Deployment

    April 17, 2026

    AI in Cloud Infrastructure: Smarter Automation & Optimization

    March 11, 2026

    Shared vs VPS vs Cloud Hosting | Which Is Best ?

    January 15, 2026
  • Blockchain & Web3
    1. Web3 Fundamentals
    2. View All

    Web3 | How Decentralization Is Changing the Internet in 2025

    October 13, 2025

    Web3 | How Decentralization Is Changing the Internet in 2025

    October 13, 2025
  • Web Development
    1. HTTP & APIs
    2. Frontend Development
    3. View All

    Mastering HTTP 2.0: Enhancing Performance and Security for Modern Web Applications

    April 17, 2026

    10 Best Practices for Securing APIs in Cloud and DevOps Environments

    April 17, 2026

    10 Essential Best Practices for Securing Your APIs in 2026

    April 13, 2026

    10 Essential Best Practices for Building Secure APIs in a Cloud Environment

    March 14, 2026

    Understanding State Management in Angular: Strategies and Pitfalls

    May 21, 2026

    Understanding State Management in Angular: A Comprehensive Analysis

    May 7, 2026

    Evolution of React Server Components: Performance, Architecture, and User Experience

    May 7, 2026

    Impact of Angular’s Dependency Injection Updates

    May 3, 2026

    Understanding State Management in Angular: Strategies and Pitfalls

    May 21, 2026

    Understanding State Management in Angular: A Comprehensive Analysis

    May 7, 2026

    Evolution of React Server Components: Performance, Architecture, and User Experience

    May 7, 2026

    Impact of Angular’s Dependency Injection Updates

    May 3, 2026
  • System Design
    1. Software Architecture
    2. View All

    Microservices Architecture

    March 1, 2026

    Monolith Architecture

    January 2, 2026

    Microservices Architecture

    March 1, 2026

    Monolith Architecture

    January 2, 2026

Type above and press Enter to search. Press Esc to cancel.