gpt4 book ai didi

php - 如何通过 react js在php中发布数据

转载 作者:可可西里 更新时间:2023-10-31 23:13:19 26 4
gpt4 key购买 nike

我是 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 aspect 作为响应

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

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

最佳答案

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

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

关于php - 如何通过 react js在php中发布数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52717556/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com