Web

IntermediateLesson 7 of 1430 min, hands-on

CORS

Part of the Web course

Your frontend runs on localhost:3000, your API runs on localhost:8000, and the request that works perfectly in curl fails in the browser with a red console message about Access-Control-Allow-Origin. Nothing is broken. The browser is doing exactly what it was designed to do, and it is protecting your users from a genuine attack, and the fix is one response header on the server.

Browsers enforce a boundary called the same-origin policy: JavaScript loaded from one origin may not read responses from a different origin unless that origin explicitly permits it. ...

What you'll learn

  • same-origin policy
  • Origin
  • Preflight OPTIONS
  • Access-Control-Allow-Origin
  • Access-Control-Allow-Methods
  • Access-Control-Allow-Headers
  • Credentials mode

Work through CORS

Sign in free and open the Web course. Work through the lesson, and ask the AI tutor when you get stuck.