process Axios POST in PHP [duplicate]

雨燕双飞 提交于 2019-12-10 01:34:52

问题


I want to send a POST request to my PHP file to handle it and store the data in the database.. I'm pretty stuck at it since $_POST stays empty whatever I try..

Can someone help me sending a post request and help me how to handle it ?

My axios request:

// 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);
});  

And this is what I kinda tried in PHP, some code is commented out because not sure if it worked..

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();
*/

}

SOLUTION:

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

The object was found in php://input


回答1:


The user solved it "finding" the object in php://input

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

As pointed by STEEL, it was an issue at the PHP code, not React or Axios.




回答2:


Just need:

if(isset($_POST['text'])) { ... }


来源:https://stackoverflow.com/questions/43793176/process-axios-post-in-php

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!