I’m trying to do some ajax with Coldfusion 7.
Anything returned from a call to a CFC method gets wrapped in a wddxPacket
<wddxPacket version=’1.0′><header/><data><string>This is the return value>/string></data></wddxPacket></code>
There is no way to avoid it. What were they thinking?!
The solution is simply not to use a CFC… instead write a standard coldfusion which parses the url for your method name and returns the data you want. Don’t forget to astudiously go around squashing all the whitespace CF likes to generate. And put special traps into your application.cfc to omit any standard headers/footers. And…
The one thing consistent about CF has always been the complete absence of foresight.
Here’s my dirty workaround: “ajax.cfm”
See the comments for a much improved version!
<cfparam name="method">
<cftry>
<cfoutput>#evaluate(method & "()")#</cfoutput>
<cfcatch></cfcatch>
</cftry>
<cffunction name="hello"><cfsilent>
<cfscript>
return "hollow ddorld";
</cfscript>
</cfsilent></cffunction>
Hey John,
No need to hate CF, bad work man and all that
First off, i agree the wddx wrapper is a pain, although back in the day i used to write loads of stuff utilizing wddx, but there are many ways around it. A cfm file like you suggest is one, but lets find a better way than evaluate() \shudder\
how about this..
#foo#
personally i’d prefer to expose my model by creating a remote proxy that wraps my model methods, and if you specify returntype=”xml” on your remote methods cf won’t wrap them in a wddx packet
Now, if you were on CF8 (i know not everyone is..) you could use the returnFormat=json argument and hey presto
I’ll try that again, this time with escaped tags..
<cfparam name="url.method">
<cftry>
<cfinvoke method="#url.method#" returnvariable="foo"></cfinvoke>
<cfcontent reset="true">
<cfoutput>#foo#</cfoutput>
<cfcatch></cfcatch>
</cftry>
<cffunction name="hello">
<cfreturn "hollow ddorld">
</cffunction>
Use cfoutput instead of cfreturn in your cffunction.
CF foresight is now CF8, and CF9 that is under developpement by adobe, and bluedragon and railo which are alternative solution.
A guy at work (thanks Kevan) offered me this solution for CF7 which is much better than my dirty hack. It neatly avoids, the evaluate, the whitespace, the debugging…. noice!