获取 - 发送 POST 数据

Fetch - sending POST data

我的 PHP 脚本没有通过 POST 从 fetch 接收任何东西。我做错了什么?

Javascript:

fetch("http://127.0.0.1/script.php", {
    method: "post",
    body: "inputs=" + JSON.stringify({param: 7})
  }).then(response => response.json()).then(response => {
    console.log(response);
});

script.php:

  echo "\nprinting POST:\n";
  print_r($_POST);

  echo "\nprinting GET:\n";
  print_r($_GET);
  die();

控制台:

printing POST: Array ( )

printing GET: Array ( )

您没有添加任何 url 参数(因此 $_GET 为空)并且您没有设置 Content-Type header 使 PHP自动解析请求body。 (因此 $_POST 为空)。

该特定测试 PHP 文件的 htaccess 文件有问题。我用它来向主项目 folder/PHP 文件发布一些东西,一切正常。