所以我在客户端有一个 Controller :
$scope.authenticate = function() {
var creds = JSON.stringify({email: this.email, password: this.password});
$http.post('/authenticate', creds).
success(function(data, status, headers, config) {
// etc
}).
error(function(data, status, headers, config) {
// etc
});
};
在服务器端:
app.post('/authenticate', function(req, res) {
console.log("Unserialized request: " + JSON.parse(req));
});
但是当我尝试解析请求时出现错误。我不知道为什么。有什么想法吗?
最佳答案
使用 express.bodyParser 中间件,它将为您进行解析,并为您提供 req.body
作为准备就绪的对象。
var express = require('express');
app.post('/authenticate', express.bodyParser(), function(req, res) {
console.log("Unserialized request: " + req.body);
});
关于javascript - 如何在 Express/Node.js 中反序列化 JSON 数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19967851/