表示不读取请求有效载荷

时间:2015-06-21 07:10:47

标签: node.js express

试图让帖子工作,基本上是用户配置文件,整个对象从express(存储在mongo中)发送到角度形式,但是,我无法获取读取我发回的数据的路径(使用身体解析器)。

首先,发送部分是使用角度的帖子:

var app = new ActiveXObject("Excel.Application");
app.Visible = true;
var xls = app.Workbooks.Add();

app.Cells(1, 1).Value = "Olympia, WA, 123";
app.Cells(2, 1).Value = "Salem, OR, 3434";
app.Cells(3, 1).Value = "Boise, ID, 342";
app.Cells(4, 1).Value = "Sacramento, CA, 3";

app.Range("A1").EntireColumn.TextToColumns(
    app.Range("B1"),  // Destination
    1,                // DataType
    1,                // TextQualifier
    false,            // ConsecutiveDelimiter
    false,            // Tab
    false,            // Semicolon
    true,             // Comma
    false,            // Space
    false,            // Other
    '',               // OtherChar
    getSafeArray([
        getSafeArray([1, 2]),
        getSafeArray([2, 2]),
        getSafeArray([3, 2])
    ])                // FieldInfo
);

// ref: https://stackoverflow.com/a/5910730
function getSafeArray(jsArr) {
    var dict = new ActiveXObject("Scripting.Dictionary");
    for (var i = 0; i < jsArr.length; i++)
    dict.add(i, jsArr[i]);
    return dict.Items();
}

这给了我以下的POST:

$scope.saveProfile = function(empty) {
    $scope.user.updateMe = false;
    $http({
        method: 'POST',
        url: '/api/postProfile/'+$scope.user._id,
        data: $scope.user
    })
        .success(function(data, status, headers, config) {
            console.log("I updated");
        })
        .error(function(data, status, headers, config) {
            console.log("Big fat fail that one");
        });

    }

然而,当我尝试在路由器中使用urlencode后的正文

     {
        "startedDateTime": "2015-06-21T06:53:50.363Z",
        "time": 0,
        "request": {
          "method": "POST",
          "url": "http://localhost:29771/api/postProfile/55841857579127018071ad97",
          "httpVersion": "unknown",
          "headers": [
            {
              "name": "Accept",
              "value": "application/json, text/plain, */*"
            },
            {
              "name": "Referer",
              "value": "http://localhost:29771/internal/profile"
            },
            {
              "name": "Origin",
              "value": "http://localhost:29771"
            },
            {
              "name": "User-Agent",
              "value": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/43.0.2357.124 Safari/537.36"
            },
            {
              "name": "Content-Type",
              "value": "application/json;charset=UTF-8"
            }
          ],
          "queryString": [],
          "cookies": [],
          "headersSize": -1,
          "bodySize": 448,
          "postData": {
            "mimeType": "application/json;charset=UTF-8",
            "text": "{\"_id\":\"55841857579127018071ad97\",\"updateMe\":false,\"imageUrl\":\"https://lh3.googleusercontent.com/-XdUIqdMkCWA/AAAAAAAAAAI/AAAAAAAAAAA/4252rscbv5M/photo.jpg?sz=50\",\"familyName\":\"Bberg\",\"firstName\":\"Ben\",\"userName\":\"Ben Bberg\",\"__v\":0,\"google\":{\"email\":\"ben.bberg@cm.com\",\"name\":\"Ben Bberg\",\"token\":\"ya29.lwGTHvRHvXhnBGTYSFVztPrjlkvtq-0HenpocX0VCsUmnbMt5zpluo411-0nyCqH2xgfwO4YCTuRyA\",\"id\":\"108193570873442725868\"}}"
          }
        },
        "response": {
          "status": 0,
          "statusText": "",
          "httpVersion": "unknown",
          "headers": [],
          "cookies": [],
          "content": {
            "size": 0,
            "mimeType": "x-unknown"
          },
          "redirectURL": "",
          "headersSize": -1,
          "bodySize": -1,
          "_transferSize": 0,
          "_error": "net::ERR_EMPTY_RESPONSE"
        },
        "cache": {},
        "timings": {
          "blocked": -1,
          "dns": -1,
          "connect": -1,
          "send": 0,
          "wait": 0,
          "receive": 0,
          "ssl": -1
        },
        "pageref": "page_8"
      }
    ]
  }
}

我什么也没得到,可以看出我试图通过对象req.body来获取键,但是看起来没什么,下面是输出日志

app.post('/api/postProfile/:id', isLoggedIn, urlencodedParser, function(req, res) {
    console.log("I got called here");
    var id = req.params.id;
       console.log("I will update " + id + " with : " + req.body);
        for (var key in req.body){
            console.log(key + " value: " + req.body[key]);
        }
});

有人有任何建议吗?

1 个答案:

答案 0 :(得分:3)

经过大量的讨论,我发现了这个问题。缺少的一行是:

app.use(bodyParser.json())

问题似乎是,如果你错过了这一行,你会得到一个完全空的req.body,但是,一旦我拥有它,它一切都很完美。 希望这有助于某人:)