【问题标题】:Parsing AngularJS http.post data on server side with express & body-parser使用 express 和 body-parser 在服务器端解析 AngularJS http.post 数据
【发布时间】:2016-02-19 15:38:44
【问题描述】:

我最近才开始学习 MEAN 堆栈,如果这看起来是一个非常愚蠢的问题,请原谅我。我的问题如下:

在客户端(controller.js)我有,

  $http({
  method  : 'POST',
  url     : '/root',
    // set the headers so angular passing info as form data (not request payload)
  headers : { 'Content-Type': 'application/x-www-form-urlencoded' },
  data    :  {
              type:'root',
              username:$scope.rEmail,
              password:$scope.rPassword
            }

 })

在服务器端我有,

app.post('/root', function(req, res) {

  console.log(req.body);

  console.log(req.body.username);
});

我的控制台日志显示:

17 Nov 21:39:04 - [nodemon] starting `node server/server.js`
{ '{"type":"root","username":"testUserName","password":"testPassword"}': '' }
undefined

我想 req.body.username 给我 testUserName 但我没有定义。我得到的 JSON 格式有点奇怪。谁能帮我解决这个问题?我做了一些阅读并尝试使用 body-parser 并浏览了 angular js $http.post 文档,但没有找到任何可以帮助我的东西。

我想问题出在:

 { '{"type":"root","username":"testUserName","password":"testPassword"}': '' }

但我似乎无法弄清楚如何在我的角度控制器中传递来自 $http.post 的数据,以便我只能以标识符:值格式获取我的请求。

【问题讨论】:

    标签: javascript json angularjs express body-parser


    【解决方案1】:
    $http({
      method  : 'POST',
      url     : '/root',
        // set the headers so angular passing info as form data (not request payload)
      headers : { 'Content-Type': 'application/x-www-form-urlencoded' },
      params    :  {
                  type:'root',
                  username:$scope.rEmail,
                  password:$scope.rPassword
                }
    
     })
    

    用 'params' 试试,也许不行,但试试看:P

    【讨论】:

      【解决方案2】:

      没关系,我想通了。看来我需要从编码中休息一下。

      headers : { 'Content-Type': 'application/x-www-form-urlencoded' }
      

      headers : { 'Content-Type': 'application/json' } 
      

      解决了问题。

      【讨论】:

        【解决方案3】:

        在下面试试我的源代码:

        $http({
          method  : 'POST',
          url     : '/root',
            // set the headers so angular passing info as form data (not request payload)
          headers : { 'Content-Type': 'application/x-www-form-urlencoded' },
          data    :  {
                      type:'root',
                      username:$scope.rEmail,
                      password:$scope.rPassword
                    },
          transformRequest: function(obj) {
              var str = [];
              for(var p in obj){
                  str.push(encodeURIComponent(p) + '=' + encodeURIComponent(obj[p]));
              }
              return str.join('&');
          }
        
         })
        

        【讨论】:

          【解决方案4】:

          我尝试使用“params”而不是“data”并且工作正常,并且标题是“application/x-www-form-urlencoded”还是“application/json”都没有关系 但是在节点上使用“application/json”与 request.query.param1 一起工作。

          【讨论】:

            猜你喜欢
            • 2016-10-14
            • 2016-07-25
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2015-05-15
            • 1970-01-01
            相关资源
            最近更新 更多