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 ;)
18 Responses to “Prevent debug info from messing with your AJAX responses”
Nice! Maybe It should be incoporated to Cake so we don’t need to hack it
Thanks for the tip.
Why not do it a more “Cake” way?
In AppController::beforeFilter() add:
if ($this->isAjax()) {
Configure::write(‘debug’, 0);
$this->disableCache();
}
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.
@Mauro Zadunaisky
No problem.
I’ve added an enhancement ticket… seems like a super-easy fix.
Is it Configure::read() or Configure::read(‘debug’) ?
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
Hmm, it cut out my code snippet. Lets try again:
layouts/ajax.ctp:
echo $content_for_layout; Configure::write(‘debug’, 0);
I would remove the snippet completely from app/webroot/index.php, as your solution still has the disadvantage of messing up “normal” JSON responses.
@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.
@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.
@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).
@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.
Yay! Congrats on the new design (liked the old one better lol j/k). Nice article as always, thanks.
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!
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 ““;
}
}
@jblotus
Thank you. Fixed-up.
The timestamp echoing has been removed altogether in 1.3.3. If one needs timestamps just use the awesome DebugKit.
@ADmad
Yep, I did mention that in the update at the top.
The debug kit, is great… I wish it would work better in IE, because it causes some JS errors, which cross-browser testing a little more complicated.