我在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