I'm invoking a controller function:
$.get("http://localhost/universityapp/courses/listnames", function(data){
alert("Data Loaded: " + data);
});
And in my Controller:
public function listnames() {
$data = Array(
"name" => "Sergio",
"age" => 23
);
$this->set('test', $data);
$this->render('/Elements/ajaxreturn'); // This View is declared at /Elements/ajaxreturn.ctp
}
And in that View:
<?php echo json_encode($asdf); ?>
However, the Action is returning the entire page including the Layout content (header, footer, navigation).
What am I missing here? How can I return just the JSON data without the Layout content?
You need to disable layout like this
$this->layout = null ;
Now your action will become
public function listnames() {
$this->layout = null ;
$data = Array(
"name" => "Sergio",
"age" => 23
);
$this->set('test', $data);
$this->render('/Elements/ajaxreturn'); // This View is declared at /Elements/ajaxreturn.ctp
}