我在cakephp做了一个restfull api ... 有时我有一些抛出异常。例如:cakephp 3中的JSON异常
if (!$this->request->is('post')) {
throw new MethodNotAllowedException("The requested resource does not support http method " . $this->request->param('_method'));
}
我的问题是当网址/controller/action.json的回应是:
{
message: "The requested resource does not support http method GET",
url: "/api/auth/users/authenticate.json",
code: 405
}
JSON格式,但是,当我的网址/控制器/动作。我的回复是HTML,我想知道是否有可能强制这些例外总是json没有将.json放在url中。
谢谢!
您可以指定在响应头'应用/ json'。 – TheDude
你可以使用'$ this-> RequestHandler-> renderAs($ this,'json');' –
我在控制器中添加了我的动作,然后抛出异常...但没有成功......异常呈现作为HTML ... – jaloise