NinadNagwekar
NinadNagwekar

Reputation: 5

Json response for my cakephp application

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

Answers (1)

Borislav Sabev
Borislav Sabev

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:

  1. Cake has JSON views that you can use to return data.

  2. 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

Related Questions