【发布时间】:2020-04-28 21:07:26
【问题描述】:
我正在尝试获取我通过 fetch() 发送的发布请求的响应,但结果返回一个空文本。
JS:
async getTotalCompletionTimes()
{
var res = await fetch("repository/maps.php?method=getcompletiontimes&map="+this.getName(), {method: 'POST'});
const result = await res.text();
return result;
}
PHP
<?php
require_once("user.php");
if($_SERVER["REQUEST_METHOD"] == "POST")
{
<some code>
else if(isset($_POST["method"]) && $_POST["method"] == "getcompletiontimes" && isset($_POST["map"]))
{
$times = 0;
$users = glob('../users/*', GLOB_ONLYDIR);
foreach($users as $u)
{
if(!file_exists($u."/maps.json")) continue;
$json = json_decode(file_get_contents($u."/maps.json"), true);
foreach($json as $map => $v)
{
if($map == $_POST["map"])
{
$times += $v;
}
}
}
echo $times;
}
<some other code>
?>
我在 cmd 中使用 curl 测试了 php 响应: curl -X POST localhost/game/repository/maps.php -d "method=getcompletiontimes&map=map_1" 并返回“2”作为响应。
【问题讨论】:
标签: javascript php fetch-api