CakePHP Ajax/JSON calls fail? Try turning debug output off

ajax CakePHP is a rapid development framework for PHP that provides an extensible architecture for developing, maintaining, and deploying applications. CakePHP provides several Ajax features, but if cakephp debug is not turned off, most Ajax calls and JSON encoding would fail.

The problem / symptoms

When calling a server side cakephp method through Ajax calls usualy in combination with JSON encoding, the call fails. The server returns HTTP 200 and everything seems fine, but the Ajax call just doesn’t work.

Diagnostic

By default, cakePHP adds debug information to it’s HTML output. This debug information is very useful, but also very disruptive to Ajax calls and JSON parsing.

The solution

Turn debug off by setting it to 0 – There are two ways you can do that:

1) For the entire cakephp application – go to core.php under the app/config folder and replace this line “Configure::write(‘debug’, 2);” with this line “Configure::write(‘debug’, 0);”

2) For only a specific method call – add this line to your relevant method in the controller –

function yourAjaxCallMethod() {
         Configure::write(‘debug’, 0);

}

Share the love...Tweet about this on TwitterShare on LinkedInShare on Google+Share on Facebook

Amir Shevat

Amir Shevat is the global Startup Outreach lead in Google Developer Relations (g.co/launch). Previously, Amir Led Google Campus Tel Aviv and was the co founder of several startups.

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *