This question already has an answer here:
Stripe Checkout example running into CORS error from localhost (1 answer)
Closed 7 days ago.
I get a cors error saying Access to XMLHttpRequest at 'https://checkout.stripe.com/c/pay/cs_test_a13NBIQCPQonxjMN9GGvkS9u71VZGdxxJTZblp8mh8owCu72JIhm3vW3X6#fidkdWxOYHwnPyd1blpxYHZxWjA0T09VamtMZmZhaWx3NGZ3fHMxcXxsc0RxY05GY0QwT2JwNWhEazxsbE83dlNEQGJSdEM1bW9Sc0t8UE11S0B0cUZKfEBsdUwyZDFXTUE3UjZqSzFgbG5oNTVVPVIzaWNWcicpJ2N3amhWYHdzYHcnP3F3cGApJ2lkfGpwcVF8dWAnPyd2bGtiaWBabHFgaCcpJ2BrZGdpYFVpZGZgbWppYWB3dic%2FcXdwYHgl' (redirected from 'http://localhost:3000/api/checkout_sessions') from origin 'http://localhost:3000' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource.
I tried using rewrites in the next.config.js
file but the only problem is that the stripe session id keeps changing.
`
module.exports = {
async rewrites() {
return [
{
source: '/api/checkout_sessions',
destination: 'what do a put here?',
},
]
},
}
`
So far based off my research using next js rewrites is the only way to solve this cors error but if their is any other way, please tell me.
I have a button that sends a post request to /api/checkout_sessions and thats supposed to forward me to a stripe checkout by giving me a session id. But i get a cors error.
1条答案
按热度按时间cyvaqqii1#
有两种方法可以解决这个问题。
1.不要使用
XMLHttpRequest
或fetch()
来重定向到Checkout服务器端,而是使用触发服务器端函数的普通表单提交来创建Checkout会话,这样您就可以在服务器端使用303重定向来重定向到会话URL1.如果仍然需要使用
XMLHttpRequest
或fetch()
,可以将会话的URL返回给客户端(例如,在JSON对象中)。通过窗口.位置)。