express bodyparser post 数组项未定义
express4 bodyparser post array item is udefined
我有一个快速应用程序,它使用 bodyParser 从 post 请求中获取 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);
});
您需要将数据发送为 String, not as PlainObject:
$.ajax({
url: postURL,
data: JSON.stringify( {
"device": {
"group": $('#devicegroup').val(),
"name": $('#devicename').val()
},
"events": {
"http": {
"address": $('#httpaddress').val()
}
}
} ),
type: 'POST',
contentType: 'application/json',
dataType: 'json'
}).success(function(response) {
console.log(response);
});
我有一个快速应用程序,它使用 bodyParser 从 post 请求中获取 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);
});
您需要将数据发送为 String, not as PlainObject:
$.ajax({
url: postURL,
data: JSON.stringify( {
"device": {
"group": $('#devicegroup').val(),
"name": $('#devicename').val()
},
"events": {
"http": {
"address": $('#httpaddress').val()
}
}
} ),
type: 'POST',
contentType: 'application/json',
dataType: 'json'
}).success(function(response) {
console.log(response);
});