Ang*_*uis 0 javascript php fetch-api
我不知道为什么它不起作用。它返回空$_POST。
JS:
const updateRequest = new FormData();
updateRequest.append('updateRequest', 'next-period');
fetch('file.php', {
method: 'POST',
data: updateRequest
})
.then((response) => response.text())
.then((text) => {
console.log(text);
})
Run Code Online (Sandbox Code Playgroud)
PHP:
<?php
if($_SERVER['REQUEST_METHOD'] === 'POST') {
var_dump(empty($_POST));
}
Run Code Online (Sandbox Code Playgroud)
在 PHP 文件中,服务器请求是 POST 但 var_dump 记录为 true。
根据文档,如果您想在请求正文中将数据发送到服务器(就像 POST 一样),那么您可以将它放在body选项中。没有data可用的选项。如果您将它提供给 Fetch 方法,它不会期望它并且会简单地忽略它。也许你已经把它与 jQuery $.ajax() (它有一个data选项)的语法混淆了?
你需要写:
fetch('file.php', {
method: 'POST',
body: updateRequest //"body" instead of "data"
})
Run Code Online (Sandbox Code Playgroud)
演示(运行小提琴时观看网络选项卡以查看正文中包含的数据):http : //jsfiddle.net/2vx0rp3q/