我想发送一个POST请求到我的PHP文件处理它并将数据存储在数据库中.. 无论我尝试什么,$_POST始终保持为空,我很困扰..
有人可以帮助我发送一个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);
});
我在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();
*/
}
解决方案:$data = json_decode(file_get_contents("php://input"), true);
$task = $data['text'];
在php://input中找到了该对象