Reputation: 5
I am using cakephp 2.1 and I am a newbie. As Cakephp an ORM based, Can anyone tell me how would I get the data response in json? What steps I have to follow? please help!
Upvotes: 0
Views: 143
Reputation: 4866
This question has been answered countless times by now... A nice overall explanation can he found here.
There are two options in Cake:
Cake has JSON views that you can use to return data.
An empty layout and a view that returns JSON encoded data like:
echo json_encode($data)
Of course that data must be set prior in the Controller:
$dataFromDb = $this->Model->find('all');
$this->set('data', $dataFromDB);
And you also must set Content-Type: application/json
as a header.
Upvotes: 1