如何通过React JS在PHP中发布数据

时间:2018-10-09 09:24:13

标签: php reactjs api http-post

我是React的新手,请在react的我的帖子API调用下方查看

$ ls -la /dev/video10
crw-rw---- media    system    81,   1 2015-01-01 03:00 video10

和下面给出的PHP文件post.php代码:

updateData = datas => {
    axios
      .post("http://localhost:8080/ReactAPI/post.php", datas)
      .then(function(body) {
        console.log(body);
      })
      .catch(function(error) {
        console.log(error);
      });
}

控制台,我给出响应正文,如下所示:

<?php

header('Access-Control-Allow-Origin: *');
header("Access-Control-Allow-Methods: GET, POST, OPTIONS, PUT, DELETE");
header("Access-Control-Allow-Headers: Content-Type, Access-Control-Allow-Headers, X-Requested-With");

print_r($_POST);
?>

我将下面给出的{data: "Array↵(↵)↵", status: 200, statusText: "OK", headers: {…}, config: {…}, …} 传递给i方面

datas

请在这里帮助我,如何在post.php文件中接收{id: "41", userId: "3672367892", fullName: "Subhash Patel"} 响应

1 个答案:

答案 0 :(得分:3)

您无法通过$ _POST变量获取JSON POST数据。 请使用以下内容:

$data = json_decode(file_get_contents('php://input'), true);
print_r($data);