Skip to content Skip to sidebar Skip to footer

How To Pass Client-side Parameters To The Server-side In Angular/node.js/express

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 from a particular url (URL_O

Solution 1:

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

Solution 2:

HTTP GET method

Client:

$http.get('/login', {params: {name: 'ABCXYZ'}})
    .success(
        function(success){
            console.log(success)
        })
    .error(
        function(error){
            console.log(error)
        });

Server:

router.get('/login', function(req, res, next) {
    var username = req.query.name;
    res.json({'status': 200, 'msg': 'success'});
}

HTTP POST method

Client:

$http.post('/login', {params: {name: 'ABCXYZ'}})
    .success(
        function(success){
            console.log(success)
        })
    .error(
        function(error){
            console.log(error)
        });

Server:

router.post('/login', function(req, res, next) {
    var username = req.body.params.name;
    res.json({'status': 200, 'msg': 'success'});
}

Solution 3:

Answer vs Good Solution

  • HTTP POST is preferred while sending data to the server.

  • HTTP GET method means querying for data, not sending data. Because of that, an HTTP request with GET method will always have request.body empty. But still data can be sent to server via GET using query string. In your case:

Client

$http.get('url_to_be_hit', { name : 'Mr. X'})
    .success(function(res){ //response })
    .error(function(err){ //failure });

Server

app.get('/url_to_be_hit', function(req,res,next){
   //req.query.name
}); 

Happy Helping!

Solution 4:

You can create a JS object with your parameters, and then use jQuery's $.param (http://api.jquery.com/jquery.param/) to easily serialize them into a URL query string:

var parameters = {
   amount: 123,
   description: 'test'
};

And on your $http call:

$http.get('URL_OF_INTEREST'+'?'+$.param(parameters))
        .success(
            function(success){
                console.log(success)
            })
        .error(
            function(error){
                console.log(error)
            });

EDIT: OR if you don't want to use jQuery:

$http.get('URL_OF_INTEREST', { params: parameters })
        .success(
            function(success){
                console.log(success)
            })
        .error(
            function(error){
                console.log(error)
            });

On server-side, just use the req object to get the parameters:

var amount = req.query.amount;
var description = req.query.description;

Post a Comment for "How To Pass Client-side Parameters To The Server-side In Angular/node.js/express"