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

前端 未结 4 723
孤独总比滥情好
孤独总比滥情好 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

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

提交回复
热议问题