【问题标题】:What's the difference between posting from a form and posting from a server?从表单发布和从服务器发布有什么区别?
【发布时间】:2016-02-02 12:25:24
【问题描述】:

例如,在 php 服务器上,我从 $_POST 中捕获所有发布的变量

var_dump($_POST);

如果我使用 html 表单发布到服务器,这会很好。

我尝试使用 nodejs 发布到服务器并请求:

requestify.post('http://localhost/rest/1/comment/create', {hello: 'world'})
.then(function(response) {
    console.log(response.getBody());
})

这不会被 php-server 捕获,并且 var_dump($_POST) 输出一个空数组。

【问题讨论】:

    标签: php node.js http


    【解决方案1】:

    当您提交 HTML 表单时,数据将使用application/x-www-form-urlencoded 格式进行编码(除非您告诉它使用text/plainmultipart/form-data)。

    PHP 会自动将application/x-www-form-urlencodedmultipart/form-data 解析成$_POST

    Restify 会将数据编码为application/json,PHP 默认不会解析。 This question 详细介绍了如何读取 JSON 格式的 POST 请求。

    【讨论】:

    • 不错!这将是更好的做法:从节点发布 x-www-form-urlencoded 中的数据或在 php 中处理它?
    • @Himmators — 这在很大程度上是一个见仁见智的问题。
    【解决方案2】:

    您可以使用json_decode(file_get_contents("php://input"), true);sn-p 从 PHP 读取 json 请求。 php://input 是 PHP 的标准输入,原始请求可用。详情也可在docs 中获得。

    【讨论】:

      猜你喜欢
      • 2018-08-21
      • 1970-01-01
      • 2019-11-19
      • 2011-01-05
      • 1970-01-01
      • 2023-03-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多