在PHP中处理Axios POST [英] process Axios POST in PHP

查看:637
本文介绍了在PHP中处理Axios POST的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想向我的PHP文件发送一个POST请求来处理它,并将数据存储在数据库中.由于$ _POST不管如何都为空,所以我对此非常棘手.

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 ?

我的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中尝试过的方法,因为不确定它是否起作用,所以注释了一些代码.

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

}

解决方案:

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

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

The object was found in php://input

推荐答案

用户解决了它找到" php://input

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

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

指出,这是PHP代码的问题,而不是React或Axios.

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

这篇关于在PHP中处理Axios POST的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆