【问题标题】:How to post data in php via react js如何通过 React js 在 php 中发布数据
【发布时间】:2019-03-14 00:39:03
【问题描述】:

我是 react js 新手,请参阅下面我在 react 中的 API 调用帖子

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

和PHP文件post.php代码如下:

<?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: {…}, …}

我将下面给出的datas 传递给 i 方面作为响应

{id: "41", userId: "3672367892", fullName: "Subhash Patel"}

请帮我看看如何在 post.php 文件中接收datas 响应

【问题讨论】:

    标签: php reactjs api http-post


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-03-15
      • 2012-08-01
      • 2020-05-05
      • 2020-10-24
      • 2013-10-23
      • 2023-03-30
      • 1970-01-01
      • 2016-09-15
      相关资源
      最近更新 更多