Prevent debug info from messing with your AJAX responses

Update (05/21/2010): Well, looks like it has been removed from the core completely (good riddance).

Just a little tidbit of code that can help you when doing any AJAX communication…

Generally you’ll notice that cake inserts a timestamp with each response, unless debug = 0 (i.e. production mode).
This, of course, is not a desirable thing if you wish to return a proper JSON object, for example.
At least in 1.3 the SQL debug is moved into a separate element so it does not interfere with the output (you’ll need a little more adjustment for 1.2).

Now, we have to fix-up app/webroot/index.php to avoid timestamp output whenever we have an AJAX response.

Towards the end of the file make the following update:

if (Configure::read() > 0) {
  if (!env('HTTP_X_REQUESTED_WITH') === "XMLHttpRequest") {
    echo "<!-- " . round(getMicrotime() - $TIME_START, 4) . "s -->";
  }
}	

Problem solved, enjoy ;)

16 thoughts on “Prevent debug info from messing with your AJAX responses

  1. Why not do it a more “Cake” way?

    In AppController::beforeFilter() add:
    if ($this->isAjax()) {
    Configure::write(‘debug’, 0);
    $this->disableCache();
    }

  2. Can’t you just put Configure::write(‘debug’, 0); in the controller method where the json object is called? That’s what I’ve been doing.

  3. Why?

    Maybe I’m missing something but I’ve always found it much easier to just set the debug to within the ajax.ctp layout.

    layouts/ajax.ctp

    Works well, and is less “hacky”.

    Hope that helps someone else,
    Nick

  4. Hmm, it cut out my code snippet. Lets try again:

    layouts/ajax.ctp:
    echo $content_for_layout; Configure::write(‘debug’, 0);

  5. @Nick

    To me that’s extra code in the layout, which is not necessary.
    Also, ajax.ctp may not be the only layout you use, but that largely depends on your app.

  6. @Daniel Hofstetter

    That timestamp is really not necessary, IMO… but it is there for now. So that’s one way to avoid having it during AJAX response.
    As far as other output, I believe RequestHandler outputs it properly, at least I never get that timestamp with XML or otherwise.

  7. @Andy

    What is $this->isAjax()? (You probably meant $this->RequestHandler->isAjax())…
    Also, I don’t see how that is more cake… to me app controller has other stuff to “worry” about.

    @Jason

    It’s the same in case of ‘debug’. (That’s what Configure::read() defaults to).

    @JamesF

    Not very DRY, other suggestions listed here are at least keeping the code from repeating itself over and over (especially when you have many ajax methods).

  8. @teknoid

    I wasn’t even thinking about the DRY part. So many times in cake i end up writing functions to avoid violating DRY, and I only end up using it once, and I spent three times as long on it. I guess I need to get better at planning what things I might use more than once.

  9. Like Andy I have also used the RequestHandler in the beforeFilter of the AppController to set layout to false and debug 0. It works to disable the comment but it also disabled debug which can be frustrating. I like your method because it removes the comment but still allows debug ;) Good job!

  10. hey quick note your code in the first example is missing a closing brace
    if (Configure::read() > 0) {
    if (!env(‘HTTP_X_REQUESTED_WITH’) === “XMLHttpRequest”) {
    echo ““;
    }
    }

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s