【发布时间】:2019-01-14 14:10:06
【问题描述】:
我正在使用以下 javascript 代码来执行 HTTP 请求:
function ready(path) {
fetch(path, {method : 'put'})
.then(function (response) {
console.log(response);
});
}
此请求在我的服务器上触发以下功能:
/**
* @Route("/some/route/{playerName}", name="ready")
* @param $playerName string
*/
public function toggleReady($playerName) {
$this->someService->readyUp($playerName);
$response = new Response(
'TOGGLE SUCCESS!',
Response::HTTP_OK,
array('content-type' => 'text/html'));
$response->send();
}
在客户端,正在调用then 并在控制台中打印响应。该响应包含正确的状态代码,但正文为空,bodyUsed 为 false。如何正确地将我想要的内容/正文发送到前端?
【问题讨论】:
-
您是否尝试将“body”属性与“method”属性一起使用? developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch
-
也试试这个:.then(response => console.log('Success:', JSON.stringify(response))) .catch(error => console.error('Error:',错误));
-
据我了解,
fetch的body属性设置了请求的主体,对我得到的响应没有影响。
标签: javascript php symfony httpresponse