Verified Jobs Live
Full Stack Web Development Intermediate Level ~3 min read

What is Cross-Origin Resource Sharing (CORS), and when does the browser trigger a Preflight OPTIONS request?

Quick Revision Summary

CORS enforces origin boundaries in browsers. Non-simple requests (JSON body, custom auth headers, PUT/DELETE) require a preliminary OPTIONS preflight to verify server permission.

Interactive Practice Mode
Formulate your thoughts or take notes before revealing the model solution.
Model Answer & Deep-Dive Explanation

CORS (Cross-Origin Resource Sharing) is a browser security mechanism based on the Same-Origin Policy (SOP). Two URLs have the same origin if they share identical protocol, domain, and port. When JavaScript on origin A tries to fetch data from origin B, CORS headers determine whether the browser permits the response to be read.

Preflight Requests (OPTIONS):

The browser automatically issues an HTTP OPTIONS preflight request before sending the actual request if the request is NOT a 'Simple Request'. A request triggers preflight if:

  • It uses methods other than GET, POST, or HEAD (e.g. PUT, DELETE, PATCH).
  • It sends custom headers other than standard CORS-safelisted headers (e.g. Authorization, X-API-Key).
  • The Content-Type is other than application/x-www-form-urlencoded, multipart/form-data, or text/plain (e.g. application/json).

Interviewer Evaluation Points & Scoring Rubric

  • 1 1. Defines same-origin policy parameters (protocol, host, port). 2. Identifies conditions that trigger preflight OPTIONS. 3. Mentions key headers (Access-Control-Allow-*).