React 无法从 PHP API 获取响应正文

时间:2021-05-23 12:50:15

标签: php reactjs api

我正在尝试向我的 PHP 文件发送 POST 请求并获得响应。响应以代码 200 发送,但是,我没有看到 PHP 返回什么。这是我的反应代码:

useEffect(()=>{
    const requestOptions = {
        method: 'POST',
        headers: {'Content-Type': 'application/json' },
        body: JSON.stringify({ title: 'Hello!' })
    }

    fetch('http://localhost:80/react/db.php',requestOptions)
    .then((response) => {
      console.log(response)
    })
    .catch(err => {
      alert('error')
    })
  },[])

这是我的 PHP 代码:

header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: GET, POST, PATCH, PUT, DELETE, OPTIONS');
header('Access-Control-Allow-Headers: Origin, Content-Type, X-Auth-Token');
header("Content-type:application/json");
echo "data";

在控制台上登录 response 返回我: enter image description here
我无法从 PHP 中获取“数据”。

0 个答案:

没有答案