【发布时间】:2017-05-11 10:58:36
【问题描述】:
在不加载模板的情况下,在 nette php 框架中返回 json 字符串或屏幕上的简单文本的最佳方法是什么?
【问题讨论】:
在不加载模板的情况下,在 nette php 框架中返回 json 字符串或屏幕上的简单文本的最佳方法是什么?
【问题讨论】:
你可以在你的presenter中使用sendJson($data)方法。
function renderDefault()
{
$data = ['hello' => 'world'];
$this->sendJson($data);
}
将输出 {"hello":"world"} 和 application/json Content-Type
【讨论】:
sendJson,您会发现它只适用于$this->sendResponse(new Responses\JsonResponse($data));,因此对于简单的文本,只需要另一种类型的响应 - TextResponse => $this->sendResponse(new TextResponse($data));