不确定发生了什么,但是当我在POST请求中到达我的控制器时,req.body参数没有显示出来。
我已经关闭了middleware.json中的csrf:
{
"middleware": {
"errorPages": {
"404": "errors/404",
"500": "errors/500",
"503": "errors/503"
},
"session": {
"module": "connect-redis",
"config": {
"ttl": 3600
},
"secret": "<some secret>"
},
"appsec": {
"csrf": false,
"csp": false,
"p3p": false,
"xframe": "SAMEORIGIN"
}
//
// "compiler": {
// "dust": "templates",
// "less": "css"
// },
//
// "static": {
// "srcRoot": "path:./public",
// "rootPath": "path:./.build"
// }
}
}
控制器代码:
app.post('/user', function (req, res) {
res.format({
json: function () {
//extract the user from the req
try {
var user = new User();
user.firstName = req.body.firstName;
user.lastName = req.body.lastName;
user.userName = req.body.userName;
user.password = req.body.password;
user.email = req.body.email;
//subsequent code omitted...
} catch (err) {
//handle error
}
}
})
});
POST请求如下所示:
POST / user HTTP / 1.1 Host:localhost:8000 Cache-Control:no-cache
{“firstName”:“John”,“lastName”:“Doe”,“userName”:“jdoe”, “password”:“somepassword”,“email”:“jdoe@jdoe.com”}
答案 0 :(得分:1)
您必须将此实例中的标头设置为Content-Type application / json
POST /user HTTP/1.1
Host: localhost:8000
Content-Type: application/json
Cache-Control: no-cache
{ "firstName":"John", "lastName" : "Doe", "userName": "jdoe", "password" : "somepassword", "email": "jdoe@jdoe.com" }
答案 1 :(得分:0)
binarygiant说的是什么。如果您无法在请求中设置标题,则可以在发布之前始终序列化JSON。