簡體   English   中英

Post方法適用於原始格式,但不適用於x-www-form-urlencoded POSTMAN

[英]Post method works in raw but not in x-www-form-urlencoded POSTMAN

這可能很愚蠢,但我希望你們能啟發我為什么這樣做

圖片:使用RAW的POST,但使用x-www-form-urlencoded會使所有值都為空

圖片:使用x-www-form urlencoded的POST

這是PHP方面

<?php
// required headers
header("Access-Control-Allow-Origin: http://localhost/mediapp/");
header("Content-Type: application/json; charset=UTF-8");
header("Access-Control-Allow-Methods: POST");
header("Access-Control-Max-Age: 3600");
header("Access-Control-Allow-Headers: Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With");

// files needed to connect to database
include_once 'config/database.php';
include_once 'Objects/user.php';

// get database connection
$database = new Database();
$db = $database->getConnection();

// instantiate user object
$user = new User($db);

// get posted data
$data = json_decode(file_get_contents("php://input"));

我也嘗試更改內容類型,但結果仍然相同。 我是否沒有正確使用POSTMAN,是否需要在php端進行更改? 我還需要將其與x-www-form-url-encoded一起使用

x-www-form-urlencoded會像

firstname=mikey&lastname=dalisay

為什么json_decode()返回null,請使用parse_str將其轉換為PHP對象

parse_str(file_get_contents("php://input"), $data);
var_dump($data);
echo $data['firstname'];

如果您想同時接受rawx-www-form-urlencoded ,則可以這樣寫

$reqBody = file_get_contents("php://input");
$data = json_decode($reqBody);
if(!$data){
  parse_str($reqBody, $data);
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM