Once upon a time in the web universe, there lived a cautious but curious soul — the Browser. She was smart, independent, and very protective of her heart (and her data). She didn't let just anyone in.
Then came the Server — confident, capable, and always ready to respond. He lived on a different origin, and that made things... complicated.
🌐 The First Encounter: A Simple Request
The Browser decided to talk to the Server across origins. Nothing too personal — just a simple GET request.
And the Server? He responded instantly. But the Browser, being security-conscious, peeked into the headers and asked:
“Did you say this just to me, or do you say this to everyone?”
If the Server replied with:
Access-Control-Allow-Origin: *
She’d roll her eyes and say:
“Breadcrumbs? You say this to anyone who asks. I can't trust that. You don’t get access to my JavaScript.”
But if he said:
Access-Control-Allow-Origin: https://herdomain.com
She’d blush (internally) and whisper:
“Okay… I’ll let my frontend see your response.”
🔍 The Serious Talk: Preflight Requests
Now, if things got more serious — say, a POST request with custom headers — the Browser didn’t dive in right away. Oh no. She sent a preflight OPTIONS request:
“Before I let you in, tell me how you’ll behave. What methods? What headers? Are you safe?”
If the Server didn't know how to respond (or didn’t include the right CORS headers), she ghosted him. Fast.
But even when he got it right, she kept asking every single time.
“Just double-checking. You sure you’re not faking it?”
Eventually, the Server got frustrated.
“You know I’m real. Can you stop questioning me every time?”
And she replied:
“Fine. If you want peace, tell me how long I can trust you.”
So he finally added:
Access-Control-Max-Age: 86400
And she agreed to stop preflighting for a whole day. Trust... was finally built.
🍪 The Red Flag: Sharing Cookies
The Server once tried to get a bit too close. He wanted cookies and credentials.
So the Browser raised her eyebrow:
“You want cookies? Better say you’re only here for me.”
If he replied with:
Access-Control-Allow-Origin: *
Access-Control-Allow-Credentials: true
She slammed the door.
“You can’t ask for credentials and still act like you love everyone. Pick one.”
Only when he sent:
Access-Control-Allow-Origin: https://herdomain.com
Access-Control-Allow-Credentials: true
did she say:
“Okay, I believe you. You can have my cookies.”
🧘♀️ The Moral of the Story
CORS isn’t a villain. It's the Browser’s way of protecting herself (and your users). The Server just needs to be honest, intentional, and specific about who he’s talking to and what he wants.
So next time your CORS request fails, just remember:
It’s not a bug — it’s a boundary.
Did you enjoy this little love story? Should I keep it going? 💌
This post is the first episode of a blog series where I explain complex web concepts through storytelling.