发布xmlhttprequest不正确传递参数

发布于 2025-02-08 12:17:53 字数 816 浏览 2 评论 0原文

我正在尝试使用xmlhttprequests提出条纹帖子请求。我的身体没有正确地发送。我尝试了很多替代方案,似乎没有任何作用。为什么不识别参数?

let xhr = new XMLHttpRequest();
xhr.open("POST", "https://api.stripe.com/v1/payment_intents");
xhr.setRequestHeader("Accept", "application/json");

xhr.setRequestHeader("Content-Type", "application/json");
xhr.setRequestHeader('Authorization', 'Bearer sk_test_keygoeshere');

xhr.onload = () => console.log(xhr.responseText);

let data = `{
"amount": 1000,
"currency": 'usd',
"payment_method_types": ['card'],
}`;

xhr.send(data);

我不断遇到以下错误:

{
  "error": {
    "code": "parameter_missing",
    "doc_url": "https://stripe.com/docs/error-codes/parameter-missing",
    "message": "Missing required param: amount.",
    "param": "amount",
    "type": "invalid_request_error"
  }
}

I am trying to make a Stripe POST request using XMLHTTPRequests. My body is not being sent properly. I've tried a lot of alternatives and nothing seems to work. Why won't it recognize the parameter?

let xhr = new XMLHttpRequest();
xhr.open("POST", "https://api.stripe.com/v1/payment_intents");
xhr.setRequestHeader("Accept", "application/json");

xhr.setRequestHeader("Content-Type", "application/json");
xhr.setRequestHeader('Authorization', 'Bearer sk_test_keygoeshere');

xhr.onload = () => console.log(xhr.responseText);

let data = `{
"amount": 1000,
"currency": 'usd',
"payment_method_types": ['card'],
}`;

xhr.send(data);

I keep getting the following error:

{
  "error": {
    "code": "parameter_missing",
    "doc_url": "https://stripe.com/docs/error-codes/parameter-missing",
    "message": "Missing required param: amount.",
    "param": "amount",
    "type": "invalid_request_error"
  }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

情深如许 2025-02-15 12:17:53

首先,您绝不应该使用秘密的API密钥客户端。如果您这样做,任何人都可以访问它,然后在您的帐户中获得完整的API访问。因此,Stripe提供了一双API键。一个是可发布的API键pk_test_123,用于制作呼叫子集客户端的子集,例如收集卡详细信息。另一个是秘密API密钥,sk_test_123,并且一个可以安全地存储在服务器上您的代码正在执行),客户或创建退款

您要编写的代码应在服务器上运行。而且,您应该使用您喜欢的服务器端语言中Stripe的7个官方客户库之一,如在这里。

如果您这样切换,则无需修复共享的代码。但是在这里解释这个问题并没有什么坏处,因为它可以更好地了解Stripe的API总体。

Stripe的API 返回JSON ,但作为输入,它不支持JSON。相反,它希望数据以application/x-www-form-urlencoded的大部分API发送。因此,目前,您发送的JSON数据被忽略了,他们的服务器只会看到您post/v1/payment_intents没有参数。

您需要做的是首先切换请求的content-type。然后,您需要确保使用表单或编码这些符号正确编码的参数。而且,如果要发送嵌套参数,则必须使用括号符号,例如shipping [address] [line1]

总的来说,尽管这仍然是错误的方法,并且使用Stripe的官方库服务器端确实是您想要的。

First off, you should never use your Secret API key client-side. If you do, anyone can access it and then have full API access on your account. Stripe offers a pair of API keys for that reason. One is the Publishable API key pk_test_123 and is used to make a subset of calls client-side such as collecting card details. The other one is the Secret API key, sk_test_123 and that one is stored securely on your server where you'll make the majority of API requests such as creating a PaymentIntent (what your code is doing), a Customer or creating a Refund.

The code you are trying to write should run on your server. And you should be using one of Stripe's 7 official client libraries in your favourite server-side language as documented here.

If you switch that way, you won't have to fix the code you shared. But it doesn't hurt to explain the problem too here since it gives a better understanding of Stripe's API overall.

Stripe's API returns JSON but as the input it doesn't support JSON. Instead, it expects the data to be sent as application/x-www-form-urlencoded for the majority of the API. So right now, the JSON data you are sending is ignored and their server only see you hit POST /v1/payment_intents with no parameters.

What you'd need to do is switch the Content-Type of your request first. Then, you'd want to ensure you pass the parameters properly encoded either using a form or URL encoding those. And if you were to send nested parameters you'd have to use the bracket notation such as shipping[address][line1] for example.

Overall though this is still the wrong approach, and using Stripe's official libraries server-side is really what you want.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文