【发布时间】:2014-06-08 22:32:39
【问题描述】:
我正在尝试将一些值从我的客户端 AngularJS 脚本传递到服务器端 NodeJS 脚本。我这样设置 POST 请求:
$scope.addUser = function() {
console.log($.param($scope.user));
$http({
method: 'POST',
url: '/addUser',
data: $.param($scope.user),
headers: { 'Content-Type': 'application/x-www-form-urlencoded' }
}).
success( function(response) {
console.log("success");
}).
error( function(response) {
console.log("error");
});
};
$scope.user 变量是{ name: "john", email: "doe" },当通过$.param($scope.user) 传递时,计算结果为name=john&email=doe。本来以为问题出在请求的content-type上,原来是一个JSON对象。在阅读了类似的问题后,我将 content-type 更改为 x-www-form-urlencoded,但仍然无法从 POST 请求中获取数据。
这是 POST 请求命中的服务器端 NodeJS 脚本:
app.post('/addUser', function(req, res) {
console.log(req.params);
});
我知道正在访问服务器端脚本,因为我可以打印出 req.method 等数据,但尝试打印 req.params 只会得到 { }。
为什么我的 POST 参数没有通过?
【问题讨论】:
标签: javascript node.js angularjs post express