在PHP中处理Axios POST

Lau*_*kel 15 php axios

我想发送一个POST请求到我的PHP文件来处理它并将数据存储在数据库中..我很沮丧,因为$ _POST保持空,无论我尝试什么..

有人可以帮我发送帖子请求并帮我处理它吗?

我的axios请求:

// Performing a POST request
axios.post('dev/api/post.php',{ text: text, unk: unk})
  .then(function(response){
      console.log(response);
}).catch(function (error) {
      console.log(error);
});  
Run Code Online (Sandbox Code Playgroud)

这是我在PHP中尝试过的,有些代码被注释掉,因为不确定它是否有用..

if(isset($_POST) && !empty($_POST)){
/*  
$jsonReceiveData = json_encode($_POST);
$json_output = json_decode($jsonReceiveData);
$task = $json_ouput->text;
$uniquekey = $json_output->unk;
*/
echo $_POST;
/*
$stmt = $pdo->prepare("INSERT INTO todo (id, tekst, uniquekey) VALUES ('', :tekst, :unk)");
$stmt->bindParam(':unk', '1');
$stmt->bindParam(':tekst','testing');
$stmt->execute();
*/
Run Code Online (Sandbox Code Playgroud)

}

解:

$data = json_decode(file_get_contents("php://input"), true);
$task = $data['text'];
Run Code Online (Sandbox Code Playgroud)

该对象在php:// input中找到

小智 9

用户解决了它“发现” php:// input中的对象

$data = json_decode(file_get_contents("php://input"), true);
$task = $data['text'];
Run Code Online (Sandbox Code Playgroud)

正如STEEL指出的那样,这是PHP代码中的问题,而不是React或Axios。