- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
来 self 正在做的客户:
$.ajax({
url: '/create',
type: 'POST',
data: JSON.stringify({
theme: "somevalue",
snippet: {
name: "somename",
content: "somevalue"
}
}),
complete: function (response)
{
}
});
在服务器上(node.js/express.js
)我在做:
var app = express();
app.use(cookieParser());
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: true}));
.......
...
app.post('/create', function (req, res)
{
var dataReceived = req.body;
});
我希望 dataReceived
的值是:
{
"theme" : "somevalue",
"snippet" : {
"name": "somename",
"content" : "somevalue"
}
}
dataReceived
的值是:
{
'{"theme":"somevalue","snippet":"name":"somename","content":"somevalue"}}': ''
}
这真的很奇怪,我找不到我做错了什么。有什么想法吗?
来自 BodyParser module documentation :
bodyParser.urlencoded(options)
Returns middleware that only parses urlencoded bodies. This parser accepts only UTF-8 encoding of the body and supports automatic inflation of gzip and deflate encodings.
A new body object containing the parsed data is populated on the request object after the middleware (i.e. req.body). This object will contain key-value pairs, where the value can be a string or array (when extended is false), or any type (when extended is true).
这与我的问题有关吗?
最佳答案
在你的客户端移除 Stringify
$.ajax({
url: '/create',
type: 'POST',
data: {
theme: "somevalue",
snippet: {
name: "somename",
content: "somevalue"
}
},
complete: function (response)
{
}
});
或者在服务端重新解析
app.post('/create', function (req, res)
{
var dataReceived = JSON.parse(req.body);
});
关于json - Express.js : POST data as KEY of a req. body 对象而不是 req.body 的 VALUE?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33201823/
我是一名优秀的程序员,十分优秀!