小编Aru*_*hra的帖子

无法从 $_POST 变量访问使用 JSON.stringify 序列化的表单数据

我创建了一个 React 应用程序,从中调用基于 PHP 构建的服务器。

以下是我调用 PHP 文件的方式:

const requestOptions = {
  method: 'POST',
  headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
  body: JSON.stringify({ name, username, password }),
};
console.log(requestOptions);

fetch('http://localhost/crud/requests/signup.php', requestOptions)
  .then(res => res.json())
  .then(data => console.log(data));
Run Code Online (Sandbox Code Playgroud)

这是我在 PHP 文件中的内容:

if (isset($_POST) && !empty($_POST)) {
  // do something
}
Run Code Online (Sandbox Code Playgroud)

当我打印$_POST变量时,我得到一个空数组。甚至$_RESPONSE是空的。

但是当我尝试像这样打印输入流时:

print_r(file_get_contents('php://input'));
Run Code Online (Sandbox Code Playgroud)

一切似乎都很好。谁能解释为什么会发生这种情况?我尝试在文档中阅读它并查找一些论坛和博客,但对答案不满意。

javascript php json x-www-form-urlencoded

2
推荐指数
1
解决办法
431
查看次数

标签 统计

javascript ×1

json ×1

php ×1

x-www-form-urlencoded ×1