Blocking ColdFusion Debugging for AJAX Requests

This question came in via Twitter today (why, oh why did I start monitoring all ColdFusion mentions in Twitter??) - how does one disable ColdFusion debugging when using Ajax. This is an important question as ColdFusion's default debug output can really screw with Ajax requests. Consider the simple example of loading content into a div:


I took this line from the last InsideRIA blog entry I wrote - basically it calls a ColdFusion service to reverse a string. If I enable ColdFusion debugging, the initial page looks likes so:

But when I fire off an Ajax request to reverse the string, check what comes back:

Yeah, that's so not hot. Luckily it is pretty easy to avoid. I've blogged before about how you can sniff the request headers to check for an Ajax based request. The article focuses on jQuery, but Cutter has assured me the same header exists in ExtJS. (Cutter did some more digging and he believes most JS framework peeps are standardizing on it.)

So given that we can check for an Ajax header in the request, we can use that to turn off debugging using the cfsetting tag and the onRequestStart method:

<cffunction name="onRequestStart" returnType="boolean" output="false"> <cfargument name="thePage" type="string" required="true"> <cfset var reqData = "">

&lt;!--- is it ajax ---&gt;
&lt;cfset reqData = getHTTPRequestData()&gt;
&lt;cfif structKeyExists(reqData.headers,"X-Requested-With") and reqData.headers["X-Requested-With"] eq "XMLHttpRequest"&gt;
    &lt;cfsetting showdebugoutput="false"&gt;

&lt;cfreturn true&gt;


That's it. Of course, that isn't the only way to do it. If you work with ColdFusion 8's built in Ajax UI items, you will notice they pass a URL parameter, _cf_nodebug=true. If you add that to any query string than debugging will be disabled. (Actually, it really, reall turns off debugging. See this blog entry for more details.)

You could also disable debugging by request path, or if the request is to a CFC, or if 'json' appears in the URL. You get the idea. Anyway, I hope this helps.

p.s. Don't forget - if you use ColdFire, you don't have to worry about any of this. Are you using ColdFire yet?

Like This?

If you like this article, please consider visiting my Amazon Wishlist or donating via PayPal to show your support. You can also subscribe to the email feed to get notified of new posts.