next.js 我如何在下一个带条带的js中修复cors错误?[duplicate]

unguejic  于 2022-12-12  发布在  其他
关注(0)|答案(1)|浏览(163)

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.

cyvaqqii

cyvaqqii1#

有两种方法可以解决这个问题。
1.不要使用XMLHttpRequestfetch()来重定向到Checkout服务器端,而是使用触发服务器端函数的普通表单提交来创建Checkout会话,这样您就可以在服务器端使用303重定向来重定向到会话URL
1.如果仍然需要使用XMLHttpRequestfetch(),可以将会话的URL返回给客户端(例如,在JSON对象中)。通过窗口.位置)。

相关问题