【问题标题】:SailsJS: POST requests are not resolved properly when invoking req.param() and req.file()SailsJS:调用 req.param() 和 req.file() 时未正确解析 POST 请求
【发布时间】:2014-08-21 06:04:03
【问题描述】:

我使用 PostMan 测试我的控制器,该控制器使用 req.param() 和 req.file() 从 POST 请求中读取数据。 奇怪的是,我提交的参数的顺序很重要。例如,

案例一:

file: some file attached
property1: 1

文件将被正确解析,而 property1 不会(未定义)。

案例 2:

file: some file attached
property1: 1
property2: 2

req.param('property2') 将为第一个请求返回 1(是的,1,此处没有错字),而为后续请求返回 undefined。

案例 3:

property1: 1
file: some file attached

这样,一切正常。

这是预期的行为还是错误?提前谢谢。

【问题讨论】:

    标签: javascript html node.js http sails.js


    【解决方案1】:

    使用 Skipper(Sails 中的文件上传系统),您需要在文件参数之前发送所有文本参数。您示例中的案例三应该是您始终发送请求的方式。有关更多信息,请参阅船长文档:

    https://github.com/balderdashy/skipper#text-parameters

    【讨论】:

    • 你救了我。我应该彻底阅读文档。
    【解决方案2】:

    req.param() 用于 PATH 参数。我猜你想在这里获取帖子数据,所以你应该使用req.body('key')

    【讨论】:

    • 我记得 SailsJS 不区分 GET 和 POST 参数。使用 req.param() 我可以获得其他 POST 参数。
    • 对于诸如 http:localhost.com/:id/home?property1=this 的 url,req.param() 用于 PATH 参数 (:id)。 req.query() 用于查询参数 (property1)。最后,req.body 用于 POST DATA,例如您可能正在寻找的内容。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-13
    • 2015-05-31
    • 1970-01-01
    相关资源
    最近更新 更多