I am not sure if this is the fanciest way to get the job done but it works REALLY well. What job am I referring to? I am referring to outputting data as JSON in a CakePHP view and then parsing it in a jQuery AJAX response. Its fairly easy to accomplish with the JSON parser provided by json.org. Scroll to the bottom and download/include json2.js.
Controller example:
$sizeArray = getimagesize('/path/to/really_cool_image.jpg');
$outputArray = array(
'file' => 'really_cool_image.jpg',
'width' => $sizeArray[0],
'height' => $sizeArray[1],
);
$this->set('result', $outputArray);
$this->render(null, 'ajax');
AJAX View example:
Configure::write('debug', 0);
echo $javascript->object($result);
jQuery AJAX response handler example:
function(response, status) {
imageDetails = JSON.parse(response);
imageName = imageDetails.file;
}
Thanks for your contributon, I’ve done something similar but without json2.js and without the ‘Configure::write(’debug’, 0);’ line in the view. I’m pretty sure it will help me improve the Ajax/JSON interaction within my CakePHP apps. Thanks.
I’ve Done also like thi in my Project ,Cool Buddy ,We r Waiting 4 New Posts =)
Abraham
Hi Jesse,
Do you know any easy way to do the pagination in cakephp using jQuery and JSON.
The built-in Ajax pagination of cake outputs the whole html which is not a good idea.