MediumπŸ‘€ 3-5 years 1 min read

What is CORS, why does the browser block requests, and how do you fix it correctly?

Reviewed by Gurusankar M.

Asked inAmazonCognizantAccentureWipro
#cors#preflight#same-origin#browser security#options
Report issue

⚑ Short Answer

CORS is a browser security mechanism: by default a page can't read responses from a different origin unless the server sends Access-Control-Allow-Origin (and friends). The browser may send a preflight OPTIONS first. Fix it on the SERVER by allowing the specific origin/methods/headers β€” not with a wildcard for credentialed requests.

β˜•Coffee Chat Question

Concept Made Simple

β€œWhat is CORS, why does the browser block requests, and how do you fix it correctly?”

🧠Mind Map Answer

Remember It Faster

Why→browser same-origin policy
Preflight→OPTIONS for non-simple requests
Fix→server: Access-Control-Allow-* headers
Credentials→no '*' with cookies — name the origin

πŸ”₯What If?

Think Beyond the Expected

CORS works in Postman but fails in the browser β€” why?

CORS is enforced ONLY by browsers β€” Postman/curl ignore it. The request itself succeeds; the browser blocks the page from READING the response because the server didn't return the right Access-Control-Allow-Origin. The fix is server-side CORS config, not client code.

πŸ˜‚Real World

CORS errors are a daily frontend↔API friction; the fix is always configuring allowed origins/methods/headers on the server (or gateway), and never '*' when cookies/credentials are involved.

🎯Interviewer's Expectation

Keywords they're listening for:

βœ“ browser same-origin policyβœ“ preflight OPTIONSβœ“ server-side Allow-* headersβœ“ no wildcard with credentialsβœ“ not enforced outside browsers

⚠️Common Mistakes

  • βœ—Trying to fix CORS on the client
  • βœ—Allow-Origin '*' with credentials
  • βœ—Forgetting to allow custom headers/methods

βœ…Best Practices

  • βœ“Configure CORS server/gateway-side
  • βœ“Allow specific origins for credentialed requests
  • βœ“Limit allowed methods/headers to what's needed

πŸ”Follow-up Questions

  • 1What makes a request 'simple' vs 'preflighted'?
  • 2Why can't you use '*' with credentials: include?
  • 3Where do you configure CORS β€” app or gateway?

🧩Related Technologies

Spring CORS configAPI gatewaysame-origin policy

Continue Learning with AI

Take this question deeper with your favourite AI assistant. Pick a depth, copy the prompt, or open it directly β€” AI is your learning companion, not a shortcut.

Plain-language foundations

I'm preparing for a software engineering interview and want to understand this from scratch, as a beginner.

Topic: Auth (REST APIs)
Interview question: "What is CORS, why does the browser block requests, and how do you fix it correctly?"

Please:
1. Explain the core idea in simple, plain language, using an everyday analogy.
2. Define any technical terms you use.
3. Walk through one small, concrete example.
4. Finish with a single sentence I can easily remember.

Keep the tone friendly and assume I'm new to this topic.

Was this answer helpful?

Support our platform by exploring our recommended products.

As an Amazon affiliate, purchases through these links may earn us a small commission β€” at no extra cost to you. It helps keep Full Stack Interview Guru free.

Related Questions