AJAX with Coldfusion 7

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>

This entry was posted in geek and tagged . Bookmark the permalink.

4 Responses to AJAX with Coldfusion 7

  1. Chris says:

    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

  2. Chris says:

    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>

  3. dreddy says:

    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.

  4. John says:

    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!

    
    <cfparam name="url.method">
    
    <!--- This function is placed in the "variables" scope. --->
    <cffunction name="hello" output="false">
          <cfreturn "hollow ddorld">
    </cffunction>
    
    <cfset result = "">
    <cfif structKeyExists(variables,url.method)>
          <cfinvoke method="#url.method#" returnvariable="result">
    </cfif>
    
     <!--- Clear any previously generated output and output the result. --->
    <cfsetting showdebugoutput="false">
    <cfcontent reset="true"><cfoutput>#result#</cfoutput><cfabort>
    

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>