在 angular 中使用 $http 应用 curl POST 请求

Applying a curl POST request with $http in angular

我正在阅读 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, etc etc etc) 将您在 browser/Javascript 中生成的令牌作为输入,并将其发送到 https://api.stripe.com/v1/charges 进行收费。

不得从前端(浏览器)进行调用Javascript/Angular。通过这样做,您 将您的密钥放入 public – 您的 Stripe 帐户将不再安全。前端代码中唯一应该包含的键是 public 键,它只能用于制作令牌。

查看 here for instructions on generating the token and sending it to the server, and here 以获取有关创建收费服务器端的说明。