2017-06-09 45 views
0

嗨Im非常新的节点js和我刚刚创建了一个非常简单的API来传递条带中的一些数据只是为了看到stripe.charges.create的功能,我想创建一个API通过我的令牌,并创建一个费,但我真的不知道如何获得数据hwen用户点击付费..在客户端传递数据到服务器节点

这是我的客户端

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

     } 

这是我的服务器端

var express = require('express'); 
var app = express(); 
var bodyParser = require('body-parser'); 
var stripe = require('stripe')('my publish key'); 

app.use(bodyParser.urlencoded({ extended: true })); 
app.use(bodyParser.json()); 

var port = process.env.PORT || 8080; 


var router = express.Router(); 



router.get('/', function(req, res) { 
    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 }); 
}); 


app.use('/api', router); 

app.listen(port); 
console.log('Magic happens on port ' + port); 

对不起,noob问题IM真的很新的节点JS

+0

变化'GET'到'POST'在'router.post( '/',函数(REQ,RES){' – Sravan

回答

2

api

所有变化getpost首先由于您使用从POST请求一个很好的做法您应该使用的客户端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

在您的节点服务器,在那里你定义你的路由。此外,这将是使用POST请求方法

router.post('/', function(req, res) { 
    ... 
    console.log(req.body.token); 
} 

链接到文件 Node ExpressJs req.body

+0

他应该使用'post',因为这不是一个好习惯,他会从客户端向服务器发送'post'请求。 – Sravan

+0

是的,我刚刚看到他在发送邮件后发送来自客户端的POST – XPLOT1ON

相关问题