【发布时间】:2017-10-03 02:13:55
【问题描述】:
我正在 cakephp 中做一个 restfull api... 有时我有一些抛出异常。例如:
if (!$this->request->is('post')) {
throw new MethodNotAllowedException("The requested resource does not support http method " . $this->request->param('_method'));
}
我的问题是当 url 是 /controller/action.json 时响应是:
{
message: "The requested resource does not support http method GET",
url: "/api/auth/users/authenticate.json",
code: 405
}
json 格式,但是,当我的 url 是 /controller/action.我的回应是 HTML,我想知道是否可以强制这些异常始终为 json 而无需将 .json 放入 url。
谢谢!
【问题讨论】:
-
您可以在响应头中指定
application/json。 -
你可以使用
$this->RequestHandler->renderAs($this, 'json'); -
在抛出异常之前,我将它添加到控制器中的操作中......但没有成功......异常呈现为 HTML......
标签: json exception cakephp-3.0 httpresponse