I admit it, I rely on the hints available in CFElipse. And, for that reason, I never knew that the cffunction tag had an optional argument, new in ColdFusin 8, which controls the format in which data is returned on remote calls to functions. I ran into this argument accidentally. I was trying to call a function on a CFC remotely. For example, let’s say we have this basic CFC:
<cfcomponent> <cffunction access="remote" name="runTest" output="false" returntype="struct"> <cfset var data=StructNew() /> <cfset data.foo="bar"/> <cfset data.bar="foo"/> <cfset data.now=now()/> <cfset data.meaning=42/> <cfreturn data/> </cffunction> </cfcomponent>
For those who don’t know, you can call remote functions on CFCs directly over HTTP. Just browse directly to the CFC and append a URL argument named “method” that names the method. For example:
Well, the quickest solution, and the only one I knew until a week or two ago, is to add a “returnFormat” argument to the URL. Using this argument you can specify one of three formats: WDDX, plain and JSON. This will force the remote call to return data in the format you specified.
So, if you wanted to return your data in JSON format your URL would look like this:
So, this works like a charm but you have to remember to add the returnFormat argument to the URL.
Well, last week, I just happened to be looking at the documentation for the cffunction tag and what do I notice? ColdFusion 8 introduced an attribute on the cffunction tag called returnFormat. And, much to my happiness, it works exactly the same as the URL argument on remote calls! I guess it goes to show you learn something new every day.