Redirect from route handler blocked by CORS policy #61451
Unanswered
FabioDainese
asked this question in
Help
Replies: 5 comments
-
The same problem |
Beta Was this translation helpful? Give feedback.
0 replies
-
i have the same issue, any workaround that ? |
Beta Was this translation helpful? Give feedback.
0 replies
-
facing the very same issue.. any solution yet? |
Beta Was this translation helpful? Give feedback.
0 replies
-
same |
Beta Was this translation helpful? Give feedback.
0 replies
-
How are you calling your API route from the frontend? Instead of this: <button
onClick={() => {
fetch("/api/your-endpoint", { method: "POST" })
}}
>
Submit
</button> Do this: <form action="/api/your-endpoint" method="post">
<button type="submit">Submit</button>
</form> |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
Summary
So, basically, I've setup a NextJS v14.1.0 to just experiment with various Stripe checkout methods, like using server actions, route handlers, etc. and everything works fine except for the route handler variant, since I get the error
access to fetch at ... from origin ... has been blocked by CORS policy
.Now, it makes sense that it raises this issue due to the fact that I'm trying to redirect the user to another separate website - hosted by Stripe - but at the same time then, why the same redirect works on server actions? I mean, they are both on the server side and they both redirect to a different website 🧐 What am I missing here?
Route handler:
Server action:
I mean, to fix this, I could simply return the url generated by Stripe - instead of directly performing a redirect - and then redirect the user from the client component with
window.location.replace(...)
, but at the same time I would really like to know why it works on one and not on the other.Finally, I've also added a sandbox link, so you can easily try this error on a live project (if you fork/download it, keep in mind to also create the
.env.local
file following.env.example
boilerplate file).Additional information
No response
Example
https://codesandbox.io/p/devbox/infallible-microservice-pkmz9g?file=%2Fapp%2Fapi%2Fstripe%2Fcreate-checkout-session%2Froute.ts%3A42%2C1
Beta Was this translation helpful? Give feedback.
All reactions