I am facing some problems with CakePHP 2.4 in the moment while working with AJAX and JSON.
I want to render data with a view, but save the resulting html as a string in a variable. After that, I would like to set an array, containing this html string among other data to return as JSON object. Unfortunately I didn't find the right way yet.
My controller code so far makes use of the CakePHP json magic:
//Controller (just parts)
$data = $this->paginate();
if($this->request->is('ajax')) {
$jsonResponse = array(
'jobs' => $data,
'foci' => $foci,
'jobTypes' => $jobTypes,
'count_number'=> $count_number
);
$this->set('jsonResponse', $jsonResponse);
$this->set('_serialize', 'jsonResponse');
} else {
// render regular view
$this->set(compact('data', 'foci', 'jobTypes', 'count_number'));
}
This outputs the perfect json in the javascript console, besides the fact, that the data in $data is plain data.
Is it somehow possible, to pass $data to a view, render it, save the output to a string variable $html, and pass $html to jobs in jsonResponse instead of $data?