2

我使用 jQuery 调用了以下非常简单的 CFC。它在CF9上本地运行良好,但客户端在CF7上,并且CF7中的cffunction中没有returnformat属性。我怎样才能使这项工作?我尝试在返回的结构上使用 SerializeJSON() ,但这不起作用。谢谢。

<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>

4

3 回答 3

0

包括toJSON.cfc,然后使用其中的方法来序列化您的结构。

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

我从未使用过 toJSON.cfc;我一直使用较旧的 JSON.cfc,但找不到指向它的链接。我不确定它是否可以处理包含查询的结构,我想您所能做的就是尝试一下。

编辑:这是我引用的 JSON.cfc:http ://www.epiphantastic.com/cfjson/downloads.php

只需这样做:

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

您可以传递更多参数,我只是从未使用过它们。默认值非常好。

于 2012-01-26T15:39:01.147 回答
0

我想如果你这样做: -

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

然后通过 GET 调用它应该可以工作。

诚然没有测试。

于 2012-01-26T15:57:35.150 回答
-1

尝试使用来自 CFLib.org 的 jsonencode 和 jsondeencode

于 2012-01-26T15:40:40.010 回答