我创建了一个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));
以下是PHP文件中的内容:
if (isset($_POST) && !empty($_POST)) {
// do something
}
当我打印$_POST
变量时,我得到一个空array.甚至$_RESPONSE
都是空的.
但当我试图像这样打印输入流时:
print_r(file_get_contents('php://input'));
一切似乎都很好.有人能解释为什么会这样吗?