do this, specify the variables that you want to display in the array, say $data , then pass this array to the view using the $this->set('data', $data); method $this->set('data', $data); and then create the view /General/SerializeJson.ctp . In this view file, put <?PHP echo json_encode($data); ?> <?PHP echo json_encode($data); ?> after that, you can use $this->render('/General/SerializeJson'); and it should output json.
Common code ...
/Controllers/MyController.php
public class MyController extends AppController { public function ajaxAction() { $data = Array( "name" => "Saad Imran", "age" => 19 ); $this->set('data', $data); $this->render('/General/SerializeJson/'); } }
/Views/General/SerializeJson.ctp
<?PHP echo json_encode($data); ?>
source share