将客户端数据传递到服务器节点

时间:2017-06-09 09:06:10

标签: javascript node.js

嗨,我是非常新的节点js,我刚刚创建了一个非常简单的API来传递我的一些条带数据只是为了看到Click的功能,我想创建一个api来传递我的令牌并创建一个收费,但我真的不知道如何获取用户点击付费的数据..

这是我的客户端

stripe.charges.create

这是我的服务器端

var handler = StripeCheckout.configure({
            key: 'MY publiskKEY',
            image: 'img/logo.jpg',
            locale: 'auto',
            token: function(token) {
                // You can access the token ID with `token.id`.
                // Get the token ID to your server-side code for use.
                $http({
                    method: 'POST',
                    url: 'http://localhost:8080/api',
                    data: {
                        token: token.id
                    }


                }).success(function(data) {


                }).error(function() {



                });
                console.log(token)
                console.log(token.id)

            }
        });

        $scope.stripeForm = function(e) {

            handler.open({
                name: 'Sample Form',
                description: 'Payment',
                amount: 1000
            });


            // Close Checkout on page navigation:
            window.addEventListener('popstate', function() {
                handler.close();
            });

        }

很抱歉noob问题我只是在节点js

中真的很新

2 个答案:

答案 0 :(得分:2)

首先在get

中将post更改为api

由于您使用的是来自客户端的帖子请求,因此您应该使用post

现在,您将从 req.body 获取您从客户端发送的数据,因为您使用 body-parser 附加了一个正文

因此,您可以将您的令牌用作 req.body.token

router.post('/', function(req, res) {

    // here you can use your request object
    // like req.body.token  req.body
    console.log(req.body)
    console.log(req.body.token)

    var charge = stripe.charges.create({
    amount: 1000,
    currency: "usd",
    description: "Example charge",
    source: 'This is i want to store my token.id',

}, function(err, charge) {
    console.log(err, charge)
});
    res.json({ token: token });
});

答案 1 :(得分:1)

在节点服务器上,您可以在其中定义路由。此外,使用POST请求方法

是一个好习惯
router.post('/', function(req, res) {
    ...
    console.log(req.body.token);
}

链接到文档 Node ExpressJs req.body

相关问题