在我的html页面中,我将此帖子发送到我的sails服务器,但由于req.param()函数没有返回任何有意义的答案,我无法访问控制器中的数据。
这是网页代码
$.post("http://myserver.local/calendar/batch",
{"batch":[
{
"start_date" : "2014-06-27T09:00:00Z",
"title" : "abc",
"attendees" : [
"Fred Bloggs",
"Jimmy Jones",
"The Queen"
],
"event_id" : "123",
"location" : "Horsham, United Kingdom",
"end_date" : "2014-06-27T10:30:00Z"
},
{
"start_date" : "2014-06-27T09:00:00Z",
"title" : "another",
"attendees" : [
"Fred Bloggs",
"Jimmy Jones",
"The Queen"
],
"event_id" : "def",
"location" : "Horsham, United Kingdom",
"end_date" : "2014-06-27T10:30:00Z"
}
]},
function (data) {
console.log("success");
}
).fail(function(res){
console.log("Error: " + res.getResponseHeader("error"));
});
这是控制器
module.exports = {
batch: function (req, res, next){
console.log("batch called");
console.log("req.body" + req.body);
console.log("req.param()" + req.param());
res.send("ok");
},
_config: {}
};
我尝试过使用req.body,但似乎不包含任何内容。 这是我在输出中得到的
batch called
req.body=[object Object]
TypeError: Cannot convert object to primitive value
at Array.toString (native)
答案 0 :(得分:12)
module.exports = {
batch: function (req, res, next){
console.log("batch called");
console.log(req.body);
console.log(req.param("batch"));
res.send("ok");
},
_config: {}
};
如果使用console.log("foo" + object)
nodejs将尝试将对象转换为字符串。只需console.log(object)