gpt4 book ai didi

javascript - 在 Angular 中使用 $http 应用curl POST 请求

转载 作者:行者123 更新时间:2023-12-03 08:43:21 30 4
gpt4 key购买 nike

我正在阅读Stripe documentation我可以通过以下curl请求向用户收费:

定义

POST https://api.stripe.com/v1/charges

请求

curl https://api.stripe.com/v1/charges \
-u sk_test_PjPAEVD1LXfUuA6XylJPnQX4: \
-d amount=400 \
-d currency=eur \
-d source=tok_16ffrPHW84OuTX9VFTYguruR \
-d description="Charge for test@example.com"

在 Angular 设置中,我认为我必须使用 $http。但是,如何传递参数呢?

我尝试了以下方法

$http.post('https://api.stripe.com/v1/charges', result)
.success(function(data, status, headers, config) {
alert("success", data);
})
.error(function(data, status, headers, config) {
alert("error", data);
});

但收到错误:

XMLHttpRequest cannot load https://api.stripe.com/v1/charges. No 'Access-Control-Allow-Origin' header is present on the requested resource.

最佳答案

您需要在服务器端运行一些代码(可能使用 PHP、Ruby、Node.JS、ASP.NET 等),以获取您在浏览器/Javascript 中生成的 token 作为输入,并将其发送到 https://api.stripe.com/v1/charges 进行收费。

不得从前端(浏览器)Javascript/Angular 进行该调用。通过这样做,您将您的 key 公开 - 您的 Stripe 帐户将不再安全。前端代码中应该出现的唯一 key 是公钥,它只能用于生成 token 。

看看here有关生成 token 并将其发送到服务器的说明,以及 here有关创建收费服务器端的说明。

关于javascript - 在 Angular 中使用 $http 应用curl POST 请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32992636/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com