简体   繁体   English

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

[英]Applying a curl POST request with $http in angular

I am reading from the Stripe documentation that I can charge an user with the following curl request: 我从Stripe文档中了解到,我可以向用户收取以下卷曲请求:

Definition 定义

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

Request 请求

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"

In an Angular setting, I presume that I have to use $http. 在Angular设置中,我假设必须使用$ http。 However, how do you pass on the parameters? 但是,如何传递参数?

I tried the following 我尝试了以下

$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);
            });

but received the error: 但收到错误:

XMLHttpRequest cannot load https://api.stripe.com/v1/charges . XMLHttpRequest无法加载https://api.stripe.com/v1/charges No 'Access-Control-Allow-Origin' header is present on the requested resource. 所请求的资源上没有“ Access-Control-Allow-Origin”标头。

You need to have some code running server-side (perhaps using PHP, Ruby, Node.JS, ASP.NET, etc etc etc) that takes the token you generated in the browser/Javascript as input, and sends it to https://api.stripe.com/v1/charges to make the charge. 您需要在服务器端运行一些代码(可能使用PHP,Ruby,Node.JS,ASP.NET等),以将您在浏览器/ Javascript中生成的令牌作为输入,并将其发送到https://api.stripe.com/v1/charges收费。

You must not make that call from the front-end (browser) Javascript/Angular. 不得从前端(浏览器)Javascript / Angular进行该调用。 By doing so, you are putting your secret key out in public – your Stripe account would no longer be secure. 这样,您就可以公开公开您的密钥 -您的Stripe帐户将不再安全。 The only key that should ever feature in your front-end code is the public key , which can only be used for making tokens. 前端代码中应该具有的唯一密钥是公用密钥 ,只能用于制作令牌。

Take a look here for instructions on generating the token and sending it to the server, and here for instructions on creating the charge server-side. 在此处查看有关生成令牌并将其发送到服务器的说明,并在此处获取有关创建计费服务器端的说明。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM