我正在尝试通过邮递员发送数据,但是post方法什么也没发送

时间:2019-02-24 07:08:38

标签: php postman

$ _ 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中打印“内容”,并且工作正常。 谁能告诉我我在哪里做错了。

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

enter image description here

3 个答案:

答案 0 :(得分:2)

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

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

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

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

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

在邮递员中,只需执行以下操作:

enter image description here

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

答案 1 :(得分:1)

使用以下内容接收数据

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

答案 2 :(得分:0)

Post字段应该通过请求正文发送,而不是通过查询字符串发送(您可以期望通过get请求接收它们)。在邮递员中,切换到正文选项卡,选择原始格式和内容类型JSON,然后将其放在正文中:

{

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

 }