Selectively turn off coldfusion's debug output on ajax requests

Author: Steven Neiland

Warning: This blog entry was written two or more years ago. Therefore, it may contain broken links, out-dated or misleading content, or information that is just plain wrong. Please read on with caution.

When I am developing a project on my local machine I normally have ColdFusion's request debugging output turned on so that I can see everything that is going on in the background. In particular I find this useful for identifying when queries are being run too often or are taking too long.

However this can be a problem when dealing with ajax requests as CF does not know not to include its debugging data in the returned request. At best this means you get too much information in any html served via ajax, at worst it breaks any ajax call that expects pure json in the response.

Selectively disabling debugout using the Request Headers

Fortunately there is a simple solution to this problem if like me (and most of the internet) you use JQuery. When making an ajax request JQuery adds a "X-Requested-With" header that we can watch for. If it is present and has a value of "XMLHttpRequest" then we can blocking the debugging output for that single request.

To do this I include this code block either in onRequestStart or in the case of FW/1 the setupRequest function.

<cffunction name="onRequestStart" access="public" output="false">
      <!--- Test for the X-Requested-With header and disabled debug output if value of XMLHttpRequest --->
      <cfset var headers = getHTTPRequestData().headers>

      <cfif structKeyExists(headers,"X-Requested-With") AND headers["X-Requested-With"] EQ "XMLHttpRequest">
            <cfsetting showdebugoutput="false" />

Reader Comments

paulr's Gravatar
Wednesday, March 9, 2016 at 10:07:41 AM Coordinated Universal Time

exactly what i was looking for, thanks

  • Please keep comments on-topic.
  • Please do not post unrelated questions or large chunks of code.
  • Please do not engage in flaming/abusive behaviour.
  • Comments that contain advertisments or appear to be created for the purpose of link building, will not be published.

Archives Blog Listing