How to pass client-side parameters to the server-side in Angular/Node.js/Express

前端 未结 4 725
孤独总比滥情好
孤独总比滥情好 2020-12-16 05:44

Probably a very basic question, but I cannot seem to find a simple answer.

I have a GET method leveraging Angular\'s $http that is requesting a promise

4条回答
  •  渐次进展
    2020-12-16 06:25

    You need to pass the data in your get call as folow:

    var data = {
        amount: 3,
        currency: 2,
        source: 3,
        description: 4
    };
    
    $http.get('URL_OF_INTEREST', data) // PASS THE DATA AS THE SECOND PARAMETER
        .success(
            function(success){
                console.log(success)
            })
        .error(
            function(error){
                console.log(error)
            });
    

    And in your backend, you can get your url parameters as folow:

    router.get('/', function(req, res, next) {
    
        var amount = req.query.amount; // GET THE AMOUNT FROM THE GET REQUEST
    
        var stripeToken = "CUSTOM_PAYMENT_TOKEN";
    
        var charge = stripe.charges.create({
            amount: 1100, // amount in cents, again
            currency: "usd",
            source: stripeToken,
            description: "Example charge"
        }, function(err, charge) {
            if (err && err.type === 'StripeCardError') {
                res.json(err);   
            } else {
                res.json(charge);   
            }
        });
    });
    

提交回复
热议问题