Sunday 19 March 2017

angularJS post form data to nodejs

I´m trying to POST data from angularJS to a nodeJS app but the body request is always empty.

Angular (v1.6.3)

vm.submitForm = function() {
    vm.formData = {name: 'test'}
    var data = httpParamSerializerJQLike(vm.formData);
        var config = {
            headers : {
                'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8;'
            }
        }

        $http.post('/submit', data, config)
        .then(
            function(response) {
                console.log(response);
            }, 
            function(response) {
                console.log(response);
            }
        );

    }

Node app.js

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

Node route.js

router.post('/submit', function (req, res, next) {
    res.send(req.body);
})

I did some research and looks like all I need is

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

but is still not working for me. thank you!



via handsome

No comments:

Post a Comment