angularJS将表单数据发布到nodejs

时间:2017-03-19 17:01:42

标签: javascript angularjs node.js

我正在尝试将数据从angularJS发布到nodeJS应用,但是正文请求始终为空。

Angular(v1.6.3)

vm.submitForm = function() {
    vm.formData = {name: 'test'}
    var data = $httpParamSerializer(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);
            }
        );

    }

节点 app.js

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

节点 route.js

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

我做了一些研究,看起来就像我需要的一样

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

但仍然不适合我。谢谢!

1 个答案:

答案 0 :(得分:0)

你可以在Angular Code中试试这个:

vm.submitForm = function() { var data = { "key1" : "value" }; $http.post('/submit', data) .then( function(response) { console.log(response); } ); }

相关问题