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

前端 未结 4 716
孤独总比滥情好
孤独总比滥情好 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条回答
  •  萌比男神i
    2020-12-16 06:45

    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;
    

提交回复
热议问题