【发布时间】:2015-09-07 20:39:49
【问题描述】:
我想了解当 http 请求如下所示时如何编写sails-js 控制器,尤其是在这里我尝试使用Content-Type: application/json 发送原始数据。请指教。 (我可以使用带有表单数据的 POST,在控制器中,如果读取 req.body,我将表单数据作为 json 的键值对)
var settings = {
"async": true,
"crossDomain": true,
"url": "http://localhost:1337/person/create",
"method": "POST",
"headers": {
"content-type": "application/json"
},
"data": "{'companyID':'DEV','firstName':'Radha','lastName':'A', 'otherIDs': [123,345] }"
}
$.ajax(settings).done(function (response) {
console.log(response);
});
【问题讨论】:
-
您能否更清楚地了解您的问题,我不太了解这个问题,还是您只是需要建议?
-
我的 ajax http 调用看起来如上所述,现在我想编写sails.js 控制器来读取该http 请求并将数据部分写入控制台。请指教。我面临的问题是当我的 http 请求以原始数据形式发送用户输入时。
-
尝试只传递
dataType: "json"而不是"headers": {...}reference -
在你的控制器中你应该只有
module.exports = { action: function(req, res) { sails.log.info(req.body); } } -
@VladNeacsu 谢谢你的评论是答案。