Posted in ColdFusion | Posted on 07-03-2008 | 3,238 views
Ok, so this falls in the "Not so sure this is a good idea" department. Stefan Vesterlund posted a comment on my last blog entry concerning returnformat. He asked if it was possible to change the returnFormat at runtime. I said that I didn't think it was possible, but that you could simply use returnFormat="plain" and return JSON or WDDX manually. He played around with it and discovered you could override the default behavior. Consider the following code sample.
2 <cfset var a = [1,2,4,9]>
3 <cfreturn a>
4</cffunction>
Since I've specified no returnFormat in the cffunction tag, if I hit this via the browser and don't override returnFormat in the query string, the result will be a WDDX string. Now consider this version:
2 <cfset var a = [1,2,4,9]>
3 <cfif a[1] is 1>
4 <cfset url.returnFormat="json">
5 </cfif>
6 <cfreturn a>
7</cffunction>
The code now checks the first entry in the array, and if the value is 1, it sets url.returnFormat. Surprisingly this works. I guess ColdFusion doesn't bother worrying about the returnFormat settings until the end of the method, which kinda makes sense.
I still feel weird about this one, but thought I'd pass it along.


--Dave
I don't know - it just feels wrong. :)
Personally - when I do Ajax hits, I'll put the returnformat and method in the query string:
some.cfc?method=doit&returnformat=json
and use the Form data for my actual data.