【发布时间】:2016-05-18 03:34:39
【问题描述】:
我有一个快速应用程序,它使用 bodyParser 从发布请求中获取 json 内容。如果我写出整个正文的内容,它看起来像这样:
{
'device[group]': 'TESTGROUP',
'device[name]': 'TESTNAME',
'events[http][address]': 'http://192.168.77.11/api'
}
在我写出给我不确定的事件内容之后。我做错了什么?
我的代码如下:
app.post('/settings', function(req, res) {
console.log(req.body);
console.log(req.body.events); // undefined
客户端代码:
$.ajax({
url: postURL,
data: {
"device": {
"group": $('#devicegroup').val(),
"name": $('#devicename').val()
},
"events": {
"http": {
"address": $('#httpaddress').val()
}
}
},
type: 'POST',
dataType: 'json'
}).success(function(response) {
console.log(response);
});
【问题讨论】:
-
这样试试,因为 events 是一个二维数组 console.log(req.body.events[http][address]);
-
是否安装了
bodyparser依赖项?如果没有,请先运行$npm install bodyparser --save
标签: jquery ajax node.js express body-parser