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.
Comments on: "I Learn Something New Every Day: ReturnFormat" (2)
I think half of the stuff like this is listed on Sean and Ray’s blogs. It’s almost like they sneak it in after we blog it and create an old date! (It is also possible to set the return data type inside the CFC.)
Doug, there is one issue that I have found with the JSON return format. If you are returning data from a query that needs to keep some kind of ordering (let’s say it’s the contents of a pull-down list and the query is sorted through a “showorder”), then all hell breaks loose when you return them in JSON. Following the pattern of a CF structure, all ordering is lost, and a new ordering is put in its place based on alphanumeric keys.
My way around it, is to create a custom function that would loop through a query, write out the JSON string into a variable and then return it.