One of the lesser publicized updates in ColdFusion 8 is the introduction of the returnFormat attribute for the cffunction tag. What does this do? ColdFusion Components, when executed remotely, will return data wrapped in WDDX format. This is a problem if you want to use the result in AJAX or some other non-WDDX form. ColdFusion 7 fixed this partially by making any function that had a returnType of XML return it's data unformatted.
ColdFusion 8 takes this further and allows you to explicitly state how the result data should be returned. You can specify:
- WDDX
- JSON
- plain
As you can probably guess, the "plain" format will do nothing to your result at all. This will only work if your data is a simple value. There is no such thing as a "plain" result for arrays.
What's interesting is that you can set the returnformat on the fly when you make a request. Imagine that you've requested:
http://localhost/foo.cfc?method=transform
The default behavior will be the same as what you saw in ColdFusion 7. If the returntype isn't XML, you get WDDX. But now you can change this with the request:
http://localhost/foo.cfc?method=transform&returnFormat=JSON
This will return the same data, but encoded in JSON instead of WDDX. This URL,
http://localhost/foo.cfc?method=transform&returnFormat=plain
Will return the data as is - not encoded at all. An interesting use of this would be a service that lets you embed an ad or other "widget". I could build a "Ray's Daily Tip" service that could be embeddable with a CFHTTP call.
Archived Comments
Ray,
Does this mean I can create a cfc that reads from a database, set the return type to JSON, and then use the results with Spry without having to create a separate file?
Andrew
Oh cool. I didn't realize you could specify that in the URL. Very cool!
@Ben - And FORM as well (just being anal here).
@Andrew - Yes. Actually, you don't need to set return type to json. Just set it to ... whatever. And use returnFormat of json.
Cool. This makes working with Spry much simpler. Thanks.
I need CF8 *so* badly. There are just too many kick arse things in it. I don't know what I'll do if my client decides not to purchase it.
this is such a good feature, totally missed this in the docs as well!