获取不返回数据

问题描述 投票:0回答:1

Javascript:

fetch("https://localhost/data.php", {
  method: "POST",
  body: JSON.stringify({
    data1: data1,
    data2: data2
  })
})
.then(res => res.json())
.then(json => function (json) {
  console.log("Successful fetch.");
  console.log(json);
})
.catch((error) => function (error) {
  console.log("Failed to fetch. " + error);
});

数据.php:

<?php
echo '{
    reply: true
}';
?>

我正在尝试使用带有 fetch 的 post 请求,但我无法从 PHP 文件中得到回复,但我可以从浏览器访问它。

javascript
1个回答
-1
投票

错误在这一行

.then(json => function (json) {

fetch("https://localhost/data.php", {
    method: "POST",
    body: JSON.stringify({
      data1: data1,
      data2: data2
    })
  })
  .then(res => res.json())
  .then(json => {
    console.log("Successful fetch.");
    console.log(json);
  })
© www.soinside.com 2019 - 2024. All rights reserved.