Quick Ajax Debugging Tip

Ok, so this has been bugging me for the past few hours so I thought I’d share a quick hack. I’ve got a prototype I’m working on for a client that has a bit of Ajax in it. My Application.cfc code comes from a snippet I have in ColdFusion Builder and has the following onError:

<cffunction name="onError" returnType="void" output="false"> <cfargument name="exception" required="true"> <cfargument name="eventname" type="string" required="true"> <cfdump var="#arguments#"><cfabort> </cffunction>

Short and simple. An ugly dump is fine for testing. However, when an Ajax called was made, Firebug displayed a butt load of HTML (the dump) and it was difficult to find the exact message. I just changed it to the following code and it was incredibly helpful. Still not something I’d recommend on production, but I may change my snippet to support this:

<cffunction name="onError" returnType="void" output="false"> <cfargument name="exception" required="true"> <cfargument name="eventname" type="string" required="true"> <cfif structKeyExists(url, "method")> <cfoutput> message=#arguments.exception.message# detail=#arguments.exception.detail# </cfoutput> <cfabort> <cfelse> <cfdump var="#arguments#"><cfabort> </cfif> </cffunction>

Basically, if method exists in the URL, assume it’s Ajax. (I could also check the headers, as described here.)

Raymond Camden's Picture

About Raymond Camden

Raymond is a developer advocate. He focuses on JavaScript, serverless and enterprise cat demos. If you like this article, please consider visiting my Amazon Wishlist or donating via PayPal to show your support.

Lafayette, LA https://www.raymondcamden.com

Comments