从nodeJS获取angularJS $ http.post

时间:2016-04-18 04:30:44

标签: angularjs node.js

请帮帮我,我想获取发送到nodeJS中处理的angularJS $ http.post参数。

我想看看参数(nama,nip,pernr等等)是否成功发送,但结果是未定义的,如下所示:

angularJS代码:

// ADMINISTRATOR ========================================
.state('talentapegawai.uploadtalenta.douploadtalenta', {
    views:{
         "monitorupload": { 
            url: '/douploadtalenta',
            templateUrl: '/progressupload.html',
            controller:function($scope, $http, XLSXReaderService){
                $scope.prograssing2 = true;
                for(var i=0; i < $scope.sheets[$scope.selectedSheetName].length; i++){
                    $http.post("/execuploadtalenta",{'nama': $scope.sheets[$scope.selectedSheetName][i].nama, 'nip': $scope.sheets[$scope.selectedSheetName][i].nip, 'pernr':$scope.sheets[$scope.selectedSheetName][i].pernr, 'tgl_grade_terakhir': $scope.sheets[$scope.selectedSheetName][i].tgl_grade, 'singkatan_talenta': $scope.sheets[$scope.selectedSheetName][i].talenta_abbr, 'talenta': $scope.sheets[$scope.selectedSheetName][i].talenta, 'mulai':$scope.sheets[$scope.selectedSheetName][i].mulai, 'akhir':$scope.sheets[$scope.selectedSheetName][i].akhir})
                        .success(function(data, status, headers, config){
                            console.log("inserted Successfully");
                    });
                }
                $scope.prograssing2 = false;
            }
        }
    }   
})

NodeJS代码(快递):

var express = require('express');
var app = express();
var server = require('http').createServer(app);
var bodyParser = require('body-parser');
app.use(bodyParser.urlencoded({ extended: false }));

    app.post('/execuploadtalenta', requireLogin, function (req, res) {
        console.log("NILAI REQUEST : "+req.body.nama); -->return undefined
        console.log("NILAI REQUEST : "+req); -->return [object, object]
    });

server.listen(3333);

1 个答案:

答案 0 :(得分:1)

首先,告诉body-parser解析json请求:

app.use(bodyParser.json({limit: '10mb'}));

这也是限制json对象大小的好习惯 然后,您应该能够将主体读取为JSON对象。 采取故障排除提示,只是为了确保您在身体中获得的是正确的,您可以将其打印为字符串:

console.log(JSON.stringify(req.body));