【发布时间】:2021-08-12 01:00:25
【问题描述】:
我正在尝试向我的 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";
【问题讨论】: