Wednesday, March 17, 2010

AJAX notes on CakePHP

It's usually handy to do small ajax requests that return (or output) simple string values like 'true', 'false', 'ok', 0, 1. It's easy to test and debug because you can see the results immediately. For CakePHP you usually have to do some things before rendering your view so your output will be as espected:

Configure::write('debug', 0);
In case you are in development mode, this will prevent things like SQL logs from displaying at the end of the output

$this->layout = 'ajax';
This is CakePHP's built in layout that you can use for ajax requests, or basically this is a blank layout without the HTML head tags, etc.

This is usually forgotten and usually causes problems. I encountered this once with IE7 because it was caching one of my ajax requests. This will send proper headers so the browser will not cache the current request.