帆中提供json格式的参数 [英] Give parameter with json format in sails
问题描述
在我的控制器和其他有req对象的区域中,我可以使用req.params('username')
访问请求参数.对于通常发布的数据来说,这很好,但是我希望我的API在请求正文中接受JSON对象,并将其转换为仍然可以使用req.params()
访问的参数.
In my controllers and other areas where there's a req object, I can access request parameters using req.params('username')
. This is fine for normally POSTed data, but I want my API to accept a JSON object in the request body and convert it to parameters that I can still access with req.params()
.
例如,如果我将其作为POST请求正文发送到我的控制器操作:
So for example, if I send this as the POST request body to my controller action:
{'username': 'Chris', 'password': 'mypass'}
我希望能够使用req.params('username')
和req.param('password')
来获取用户名和密码.
I want to be able to get the username and password using req.params('username')
and req.param('password')
.
目前唯一可行的方法是发送这样的数据:
At the moment the only thing that works is sending the data like this:
username=Chris&password=mypass
有什么想法吗?
推荐答案
发送json对象时,可以访问req.body
When sending a json object, you can access the POST
request data in req.body
这篇关于帆中提供json格式的参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!