无法从请求中读取参数

时间:2015-09-01 12:31:14

标签: angularjs node.js object request

My Angular:

test/id/(ID)

我的节点:

$http.get('/api/orders', {
        params: {
            userId:userId,
            token:token
        }
     })
     .success(function (data,status) {

     });

我的结果:

  router.route('/')
        //GET all Orders
        .get(function(req, res, next) 
            {
           console.log(req);
          });

为什么我的....... . . params:{}, query :{token:'mytoken',userId:'myUserId'} . . ...... 为空,params填充了参数。我想要query中的数据。我哪里错了?

1 个答案:

答案 0 :(得分:3)

通过设置params对象,以agular方式设置$http.get方法,只需将数据添加到查询字符串即可。正如here所写:

params – {Object.<string|Object>} – Map of strings or objects which will be serialized with the paramSerializer and appended as GET parameters.

因此,它们在服务器端出现在req.query对象中是合乎逻辑的。为了在req.param对象中获取此数据,您必须将其映射到路径中。将路线更改为:

app.get('/api/orders/:userId/:token', ...);

并提出角度请求:

$http.get('/api/orders/' + userId + '/' + token })
     .success(function (data,status) {

     });
相关问题