JSON and CakePHP

Last few articles in this blog are only for stupid problems and unfortunately this one will be the same.

I had to use JSON to update some chained select boxes. I am using one jQuery plugin for this. The problem is that if you code, most likely your debug setting in the core.php file is set to 1 (or 2). This way the developer can see errors from CakePHP.

When the level is set to 1, at the end of the page, there is an comment, displaying the processing time for the page, but when you use JSON, this comment causes troubles (probably, because JSON doesn’t accept this type of comments) and the JS fail to process the response.

In order to check JSON, I needed to disable set debug level to 0 and after this to reset it again to 1 (for debugging purposes in other parts of the application).

So, the conclusion: bear in mind, that JSON doesn’t like HTML comments.

Hope this helps.

Share it:
  • Facebook
  • Twitter
  • Digg
  • StumbleUpon
  • del.icio.us
  • Google Bookmarks
  • Yahoo! Buzz
  • Add to favorites
  • Identi.ca

6 thoughts on “JSON and CakePHP

  1. If you are using Router::parseExtensions to handle the mapping of .js file extensions you can simple put a Configure::write(‘debug’, 0); in the layout for the js extension.

    Then it will never be a problem.
    You might also want to be more clever, and save the current setting, set it to zero and then after your script you can return it to the setting it was using previously.

  2. Thanks for the hints Abba Bryant, actually I knew about setting the Configure::write(’debug’, 0); for the specific action, but it was annoying for the first time πŸ™‚

    About parseExtensions – I’ve read about it, but I’ve never used it, I should try…

    Thanks again!

  3. As long as you know what you do I don’t see any problems in editing this file: the file is in the app folder and if you use an advanced installation you have to touch it anyway.

  4. You are right, it’s in APP, probably I need to touch, because I am tired from switching on and off debug, as well as I haven’t used this parsing time anyway. πŸ™‚

Leave a Reply

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