Cross-Origin Resource Sharing (CORS) Error: Prefli...
# general-help
g
Hello there, I'm currently facing an issue with Cross-Origin Resource Sharing (
CORS
) when attempting to access my API from a locally running Next.js app. The API itself is forwarded through Zero Trust Cloudflare Tunneling. I would greatly appreciate any help to solve this issue. Here's the scenario: - I have a Next.js app running on my local machine, which needs to communicate with my API. - Whenever I make requests to the API using the Fetch API within my Next.js app, the network tab in the browser displays a "PreflightMissingAllowOriginHeader" error related to CORS. - Interestingly, I don't encounter any CORS errors when testing the API using tools like Postman. Key details about my setup: - The Next.js app is running locally, not hosted via Cloudflare. - The API server is separate and is forwarded through Zero Trust Cloudflare Tunneling. Despite these measures, I'm still experiencing the CORS error when attempting to access the API from my Next.js app. I suspect there may be some configuration aspect related to Cloudflare or the way Zero Trust Cloudflare Tunneling handles incoming requests that I'm overlooking. I would be grateful for any suggestions, troubleshooting steps, or guidance on resolving this issue. I'm also down for providing any details if needed.

https://cdn.discordapp.com/attachments/1112154295001944174/1112154541966758059/image.png

https://cdn.discordapp.com/attachments/1112154295001944174/1112154542260363404/image.png

k
Is your Next.JS app returning CORS headers to OPTIONS requests (and returning a 2XX code for OPTIONS)?
Postman won't have CORS errors because it isn't a browser and CORS is purely a browser concept.
g
I gotta admit I have no clue what that means tbh
Ah, I see, that makes sense
CORS is just a set of headers that tells a browser what it can and can't do across an origin.
g
OH
k
If it isn't setup, and you're trying to do anything but a simple
GET
, it'll refuse to make the request.
There's an
OPTIONS
'preflight' which says "I plan to do this method with these headers, is that okay?" and your app will respond "Here's what I accept"
As long as the accepted stuff matches what it wants to do, it finally sends the
<METHOD>
request
g
That's actually quite interesting, I'm not sure how I missed that but I'll try it right away. In the meantine, thank you so much for your prompt reply as always! This community wouldn't be same without you!
Looks like the error is gone for sure, I'm not getting any error in my logs anymore even though something fishy is happening (which I honestly believe is on NextJS end). Thank Kian! ❤️
4 Views