ColdFusion AJAX -- works on CF9, how about CF7?

541 views Asked by At

I have the following, very simple CFC that I'm calling with jQuery. It works fine locally on CF9, but the client is on CF7, and there's no returnformat attribute in cffunction in CF7. How can I make this work? I tried using SerializeJSON() on the returned struct, but that didn't work. Thanks.

<cfsetting showdebugoutput="false">

<cffunction name="getPart" access="remote" returntype="any" returnformat="JSON">
    <cfargument name="myarg" type="string" required="yes">

    <cfset var ret = StructNew()>
    <cfset ret.success = true>

    <cftry>

        <cfquery name="ret.part" datasource="dsn">
        (query goes here)
        </cfquery>

        <cfset ret.recordcount = ret.part.recordcount>

        <cfcatch type="any">
            <cfset ret.success = false>
            <cfset ret.error = cfcatch>
        </cfcatch>

    </cftry>

    <cfreturn ret>

</cffunction>

3

There are 3 answers

3
Peruz Carlsen On BEST ANSWER

Try using jsonencode and jsondeencode from CFLib.org

4
Kevin B On

Include the toJSON.cfc, then use methods from it to serialize your structure.

<cfset JSON = CreateObject( "component", "toJSON" )>
<cfreturn JSON.structToJSON(ret)>

I've never used the toJSON.cfc; I've always used the older JSON.cfc, but I can't find a link to it. I'm not sure if it can handle a struct that contains a query, I guess all you can do is try it.

Edit: Here's the JSON.cfc I was referencing: http://www.epiphantastic.com/cfjson/downloads.php

simply do:

<cfset JSON = CreateObject( "component", "JSON" )>
<cfreturn JSON.encode(ret)>

There are more arguments that you can pass in, I've just never used them. The defaults are pretty good.

0
baynezy On

I think if you do:-

<cffunction ....... output="true">
    ......
    ......
    <cfoutput>#ret#</cfoutput>
</cffunction>

Then call that via GET it should work.

Admittedly not tested.