【发布时间】:2016-08-08 18:29:14
【问题描述】:
我正在使用 javascript 的 fetch() 发布数据。
fetch('/string.php', {
method: 'post',
body: JSON.stringify({
name: 'helloe world',
})
})
.then(function(response) {
if (response.status >= 200 && response.status < 300) {
return response.text()
}
throw new Error(response.statusText)
})
.then(function(response) {
console.log(response);
})
string.php 文件包含:
print_r($_POST);
现在我的问题是 $_POST 返回一个空数组。我可以通过附加文件来解决这个问题:
$_POST = json_decode(file_get_contents('php://input'), true);
但是这感觉很hackish,并不理想。这是从 JS fetch() 检索帖子数据时 PHP 的预期行为吗?
无论如何我可以自动将内容放在$_POST 中吗?
【问题讨论】:
-
对我来说看起来是正确的,您发送的是字符串的请求正文,而不是多部分表单数据。
-
删除
json.stringify比解析 php://input -
谢谢。这个问题给了我问题的答案。
标签: javascript php post