尝试使用AngularJS将对象传递给$ http请求

时间:2013-10-21 10:49:41

标签: angularjs angular-resource

我有一个像这样的对象:

$scope.user = {name: "Jim", birthday: "1/1/90", address: "123 Easy St"}

我正在对我的服务器进行$ http调用,我想传递这个用户信息。我这样想:

$http({
    url: '/api/coolLookup/' + userID, 
    method: "GET",
    params: {user:$scope.user}
  }).success(function(data){
      // do stuff
  });

在我的服务器资源中(我在这里使用ExpressJS / Node)我正在尝试访问user对象但不能:

exports.coolLookup = function (req, res) {
    console.log("Here's the user")
    console.log(req.params.user)
}

req.params.user在我的控制台中返回undefined。知道该怎么做吗?

1 个答案:

答案 0 :(得分:1)

在Express中你应该这样做:

console.log(req.query.user);

(注意:query,而不是params

这将为您提供一个字符串,使用JSON.parse()将其转换为对象。