【问题标题】:Getting a CORS error when trying to onboard users to Stripe Connect with Node.js尝试使用 Node.js 将用户加入 Stripe Connect 时出现 CORS 错误
【发布时间】:2022-01-28 06:44:32
【问题描述】:

我正在尝试设置我们的平台,以允许用户能够使用 Stripe Connect 从我们的平台进行连接。

我正在关注链接到文档的 stripe-examples/connect-onboarding-for-standard。这是我正在使用的代码。

app.get("/api/stripe/onboard-user/refresh", async (req, res) => {
  if (!req.session.accountID) {
    res.redirect("/");
    return;
  }

  try {
    const { accountID } = req.session;
    const origin = `${req.secure ? "https://" : "https://"}${req.headers.host}`;

    const accountLink = await stripe.accountLinks.create({
      type: "account_onboarding",
      account: account.id,
      refresh_url: `${origin}/api/stripe/onboard-user/refresh`,
      return_url: `${origin}/settings/invoicing`,
    });

    res.redirect(303, accountLink.url);
  } catch (err) {
    res.status(500).send({
      error: err.message,
    });
  }
});

一切都可以找到 res.redirect(303, accountLink.url);此时它会引发错误。

从源 'https: //localhost:4200' 已被 CORS 策略阻止:对预检请求的响应未通过访问控制检查:请求的资源上不存在“Access-Control-Allow-Origin”标头。

我可以从错误消息中提取 url 并将其放入浏览器中,它可以正常进入页面,我可以完成该过程。我已经尝试更改标题但没有找到解决方案。

【问题讨论】:

    标签: node.js cors stripe-payments


    【解决方案1】:

    该错误经常发生,因为您的客户端代码正在向您的服务器发出 GET 请求,期望 AJAX/fetch 调用中的 JSON 响应,但您的服务器直接重定向。这会失败,因为浏览器不知道在这种情况下该做什么。

    通常,您需要做的是将您的客户端调用更改为不执行 fetch() 或 AJAX 等效项,而是将整页提交到您的端点,然后将重定向。或者,您需要在 Node.js 代码中返回 URL 而不是重定向,并让您的客户端从响应中提取 URL 并重定向。

    【讨论】:

    • 谢谢,这正是我所需要的。我只是将 url 从服务器返回给客户端,然后让客户端转到 url,它工作得很好。再次感谢。
    猜你喜欢
    • 2021-09-05
    • 2012-10-03
    • 2022-11-07
    • 2020-12-25
    • 2021-12-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-30
    相关资源
    最近更新 更多