【问题标题】:I am trying to send data through postman but the post method sends nothing我正在尝试通过邮递员发送数据,但 post 方法什么也没发送
【发布时间】:2019-02-25 02:10:30
【问题描述】:

$_POST 方法不从邮递员返回数据。

//print_r($POST['Mobile']); (Prints nothing)

if($_SERVER['REQUEST_METHOD']=='POST'){

$response = array();

print_r($_POST['Mobile']);

if ($db->updateCart(
    $_POST['CartData'],
    $_POST['Mobile']
    )) {
    $response['error'] = false;
    $response['message'] = "positive";
}else{
    $response['error'] = true;
    $response['message'] = "Negative";
}

echo json_encode($response);

}

我已经尝试在 print_r 中打印“某物”并且效果很好。 谁能告诉我哪里做错了。

下面是我要发送的邮递员图片。

【问题讨论】:

    标签: php postman


    【解决方案1】:

    不要使用 Params 标签,而是尝试转到 Body 并选择 form-data,然后在此处输入您的键/值对。

    我认为这与 PHP 无关。问题似乎是“参数”选项卡将您的数据作为GET 参数而不是POST 数据发送。我敢打赌,如果您 print_r($_GET) 会看到您希望在 $_POST 中看到的数据。

    编辑:同时使用参数和正文

    可以在两个地方传递设置。考虑:

    <?php
    /* index.php */
    print_r([
      '$_GET' => $_GET,
      '$_POST' => $_POST,
    ]);
    

    在 Postman 中,只需这样做:

    请注意,Params 中的变量在查询字符串中。

    【讨论】:

    • 如果我想使用 Params 怎么办,因为我试图从使用 params 的 android 应用程序发送数据。到底有没有?
    【解决方案2】:

    使用以下方式接收数据

    $data = json_decode(file_get_contents('php://input'), true);
    print_r($data);
    

    【讨论】:

      【解决方案3】:

      帖子字段应该通过请求正文发送,而不是通过查询字符串(当您可以通过获取请求接收它们时)。在 postman 中,切换到 body 选项卡,选择 raw 格式和内容类型 JSON,并将其放在 body 中:

      {
      
      "CartData" : "some data", 
      "Mobile" : "123456789"
      
       }
      

      【讨论】:

        猜你喜欢
        • 2023-02-20
        • 2018-03-17
        • 2017-07-03
        • 1970-01-01
        • 1970-01-01
        • 2019-01-20
        • 2023-03-11
        • 2021-11-15
        • 1970-01-01
        相关资源
        最近更新 更多