2

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>

RobG
  • 576
  • 2
  • 9
  • 21

3 Answers3

0

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.

Kevin B
  • 94,570
  • 16
  • 163
  • 180
  • This has lots of potential, but I'm getting an error from the safeText method in toJSON.cfc because the argument being passed in, expected to be a string, is a boolean and I'm not sure why. I also don't quite understand why it insists on passing a Root Element in with your data when none of the other methods do. – RobG Jan 26 '12 at 16:51
  • Does your query return any boolean values? You may have to convert them to string within your query. that wasn't an issue with the JSON.cfc, I'll continue looking for a link. – Kevin B Jan 26 '12 at 17:04
  • No that's just it... no booleans at all. I've been debugging/dumping/etc and I'm wondering if this thing is choking because I'm passing an array of structs. I converted the query to a struct using Ben Nadel's function, then am trying to pass that to the structToJSON function and then it dies. – RobG Jan 26 '12 at 17:12
  • I found the JSON.cfc and updated it. Also, `access="remote"` needs to be `access="public"` to remove the wddx tags. – Kevin B Jan 26 '12 at 17:13
0

I think if you do:-

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

Then call that via GET it should work.

Admittedly not tested.

baynezy
  • 6,493
  • 10
  • 48
  • 73
-1

Try using jsonencode and jsondeencode from CFLib.org

Peruz Carlsen
  • 572
  • 2
  • 10
  • This has worked the best so far, but my output starts out like this:
    {"SUCCESS":true,"RECORDCOUNT":5,"PART": Which is the same thing that happens when I would use SerializeJSON() but without returnformat="JSON" (in CF9). I'm not sure how to get rid of this.
    – RobG Jan 26 '12 at 16:50
  • hmm... Instead of `access="remote"`, use `access="public"`. `access="remote"` will return it in a webservice format. In older versions of coldfusion, the only format available was wddx. – Kevin B Jan 26 '12 at 17:12
  • Yep that was the answer! I ended up creating a separate .cfm page to call the CFC and then it eliminated the WDDX tags. Thanks! – RobG Jan 26 '12 at 17:53