2
2

More than 5 years have passed since last update.

【CakePHP】jsonを出力する

Posted at

ajaxのリクエストに対してjsonを返す方法。
CakePHP2.1以降より使える。

jsonを返す方法
$res = [
  'name'=>'suzuki',
  'age'=>34,
  'score'=>[86,57,73]
];

$this->viewClass = 'json';
$this->set(compact('res'));
$this->set('_serialize','res');
返されるjson
{"name":"suzuki","age":34,"score":[86,57,73]}
2
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2