Jun 10 2007

Returning JSON from cfc’s

Tag: ColdFusionJens @ 2:27 pm

Today when working on a new version of moo chat, I ran into a problem with cfreturn.
cfreturn returns the given string wrapped in a WDDX tagblock, and that’s no good when you want to return pure JSON.

The first solution that crossed my mind was to use cfoutput.
It works great, but having output=”true” on the function, returns a whole lot of whitespace, and personaly I don’t like that.

Then I thought, why not have a loader-function to output the returned content (JSON).
So I renamed my json function to chat, and made this new function named loader:

<cffunction name="json" access="remote" returntype="void" output="true"><cfoutput>#loader()#</cfoutput></cffunction>

The reason for having it all on one line is because of whitespace (sigh).

But it works! So now I call method=”loader”, and it works great! :)

Ps. This works for my needs because the moo chat engine uses form variables to send data, if the loader where to be used as a loader for any method, then the arguments has to be passed on to the called function as well.

[ratings]





Warning: stristr() [function.stristr]: Empty delimiter in /home/asocialp/public_html/wp-content/plugins/wassup/wassup.php on line 2093