4
\$\begingroup\$

Should I leave in <cfif reFindNoCase( "\.cfc$" , targetPage ) eq 0> or not?

Direct CFC calls are flushed when the method returns. Either way, onRequestEnd will stop debug output from being returned for AJAX requests.

Keep If Statement

Additional regular expression search on every AJAX call. Direct CFC calls return quicker. Direct CFC call may not be trimmed.

Remove If Statement (inner code executes for all AJAX requests)

Direct CFC calls' value is stored in a variable. AJAX calls to pages return quicker. All AJAX responses are trimmed.

I'm using ColdFusion 8.

<cfcomponent output=    "false">

<cfset this.name=   "AJAX Debug Output" />

<cffunction name=   "onRequestEnd"
        returnType= "void"
        hint=       "I run on the end of requests (how clever).">

    <cfargument name=   "targetPage"
                hint=   "Path from the web root to the requested page." />

    <cfif isAJAXRequest()>
        <cfsetting  showDebugOutput=    "false" 
                    enableCFOutputOnly= "true" />

        <!--- Response from CFC is already flushed to browser. Skip output. --->
        <cfif reFindNoCase( "\.cfc$" , targetPage ) eq 0>

            <!--- If AJAXResponse is not set, then generated content is response. --->
            <cfparam    name=       "request.AJAXResponse"
                        default=    "#getPageContext().getOut().getString()#" />

            <cfcontent  reset= "true" />
            <cfoutput>#trim( request.AJAXResponse )#</cfoutput>
            <cfabort />

        </cfif>

    </cfif>

</cffunction>

<cffunction name=   "isAJAXRequest" 
            output= "false"
            access= "private"
            hint=   "I check to see if the request came across with ajax headers">

    <cfif structKeyExists( getHTTPRequestData().headers , "X-Requested-With" )>
        <cfreturn true />

    </cfif>

    <cfreturn false />

</cffunction>

</cfcomponent>
\$\endgroup\$
1
  • \$\begingroup\$ I primarily call pages for AJAX requests versus calling CFCs. Removing the conditional may cause a speed bump for edge cases, but a majority of requests will run optimal code. Nice to know CF9 solved this issue with onCFCRequest. \$\endgroup\$ Commented Oct 4, 2011 at 18:01

2 Answers 2

3
\$\begingroup\$

And, if you are using CF 9.0.1 you don't have to worry about the debug output at all, on direct CFC Requests. Starting with 9.0.1, CF will automatically suppress the debug output. (Thank God)

A few years back, Ray and I were hashing out how to target a request via it's header, and came up with this:

<cfif structKeyExists(reqData.headers,"X-Requested-With") and reqData.headers["X-Requested-With"] eq "XMLHttpRequest">

That worked great, as all of the Ajax libraries (that we tested) were using the same thing. That is, until JQuery released 1.6, at which point they created a custom header type (jqXHR, I think) that they use with requests.

\$\endgroup\$
3
  • \$\begingroup\$ That's interesting. jQuery 1.6.2 and 1.6.4 send X-Requested-With headers. The reason why I created the isAJAXRequest function was to allow for inevitable change. I'm curious about the second condition. Have you found an X-Requested-With header that wasn't XMLHttpRequest? \$\endgroup\$ Commented Oct 3, 2011 at 13:11
  • 1
    \$\begingroup\$ I had to go back and check the JQuery docs again, as I was sure I had read it the other day. Sure enough, documentation of the .ajax() method options tells you this about the beforeSend option: "A pre-request callback function that can be used to modify the jqXHR (in jQuery 1.4.x, XMLHTTPRequest) object before it is sent." api.jquery.com/jQuery.ajax \$\endgroup\$ Commented Oct 4, 2011 at 10:15
  • \$\begingroup\$ Great info to digest. If I was primarily calling CFC's for AJAX requests, then I'd probably leave in the conditional. It also makes sense to remove the conditional to keep inline with CF9. \$\endgroup\$ Commented Oct 4, 2011 at 18:04
6
\$\begingroup\$

If you're using ColdFusion 9, you can use onCFCRequest to deal with this type of issue. Which version of CF are you using?

\$\endgroup\$
1
  • \$\begingroup\$ I'm using 8. Hopefully will be on 9 soon. Interesting to know that's available. \$\endgroup\$ Commented Oct 3, 2011 at 12:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.