Coldfusion 据我所知,OnCFCRequest方法功能存在问题

Coldfusion 据我所知,OnCFCRequest方法功能存在问题,coldfusion,coldfusion-10,lucee,Coldfusion,Coldfusion 10,Lucee,当我将filters参数传递给一个单独的函数以使用它进行搜索时,该示例无法正常工作 像这样 函数搜索的调用无效,第四个参数(筛选器)的类型无效,无法将字符串[{“groupOp”:“AND”,“rules”:[{“field”:“errorid”,“op”:“eq”,“data”:“1”},{“field”:“line”,“op”:“eq”,“data”:…]转换为[struct]类型的值 我这样称呼它: #SearchOptions(arguments.filters)# 从函数 <c

当我将filters参数传递给一个单独的函数以使用它进行搜索时,该示例无法正常工作

像这样

函数搜索的调用无效,第四个参数(筛选器)的类型无效,无法将字符串[{“groupOp”:“AND”,“rules”:[{“field”:“errorid”,“op”:“eq”,“data”:“1”},{“field”:“line”,“op”:“eq”,“data”:…]转换为[struct]类型的值

我这样称呼它:

#SearchOptions(arguments.filters)#
从函数

<cfset filters = {"groupOp":"AND","rules":[{"field":"template","op":"eq","data":"a"},{"field":"error_type","op":"eq","data":""}]}>

<cfdump var="#filters#">
<cfoutput>
    <cfloop from="1" to="#arrayLen(filters.rules)#" index="i">
        #i#
        <cfset dataArr = filters.rules[i]>
        <cfloop collection="#dataArr#" item="key">
            #key#   
        </cfloop>
    </cfloop>   
</cfoutput>

#我#
#钥匙#
我的应用程序中有一个oncfcrest函数

<cffunction name="onCFCRequest" access="public" returntype="void" output="true" hint="I process the user's CFC request.">
    <cfargument name="component" type="string" required="true" hint="I am the component requested by the user." />
    <cfargument name="methodName" type="string" required="true" hint="I am the method requested by the user." />
    <cfargument name="methodArguments" type="struct" required="true" hint="I am the argument collection sent by the user." />
    <cfif !structKeyExists( application.apiCache, arguments.component )>
        <cfset application.apiCache[ arguments.component ] = createObject( "component", arguments.component ).init() />
    </cfif>
    <cfset local.cfc = application.apiCache[ arguments.component ] />
    <cfinvoke returnvariable="local.result" component="#local.cfc#" method="#arguments.methodName#" argumentcollection="#arguments.methodArguments#" />
    <cfset local.responseData = "" />
    <cfset local.responseMimeType = "text/plain" />
    <cfif structKeyExists( local, "result" )>
        <cfparam name="url.returnFormat" type="string" default="#getMetaData( local.cfc[ arguments.methodName ] ).returnFormat#" />
        <cfif ( (url.returnFormat eq "json") && !structKeyExists( url, "callback" ) )>
            <cfset local.responseData = serializeJSON( local.result ) />
            <cfset local.responseMimeType = "text/x-json" />
        <cfelseif ( (url.returnFormat eq "json") && structKeyExists( url, "callback" ) )>
            <cfset local.responseData = ( "#url.callback#(" & serializeJSON( local.result ) & ");" ) />
            <cfset local.responseMimeType = "text/javascript" />
        <cfelseif (url.returnFormat eq "wddx")>
            <cfwddx action="cfml2wddx" input="#local.result#" output="local.responseData" />
            <cfset local.responseMimeType = "text/xml" />
        <cfelse>
            <cfset local.responseData = local.result />
            <cfset local.responseMimeType = "text/plain" />
        </cfif>
    </cfif>
    <cfset local.binaryResponse = toBinary( toBase64( local.responseData ) ) />
    <cfheader name="content-length" value="#arrayLen( local.binaryResponse )#" />
    <cfcontent type="#local.responseMimeType#" variable="#local.binaryResponse#" />
</cffunction>

我从Ben Nadel那里识别出这种模式。您在问题中发布的代码缺少两个重要部分。首先,由
cfinvoke
应用程序调用的方法。apiCache
对象-调用此方法失败并引发异常。其次是调用此方法的JavaScript代码

实际上,错误的原因很简单。当您传递JavaScript对象时,您的JavaScript代码可能会以字符串形式传递数据。这可能是因为您调用了
JSON.stringify
,或者是因为您使用的库的某种“魔力”将对象序列化为字符串

解决方案是对ColdFusion函数的第四个参数(根据错误消息命名为filters)调用
反序列化JSON
。我自己使用的代码如下。
bSave
只有在参数可以成功转换为结构时才会变为
true

当函数中有
cfargument
标记时,必须相应地将该函数的
type
属性更改为
string

<cfset local.struVals = {}>
<cfset local.bSave = false>

<cftry>
  <cfset local.struVals = DeserializeJSON( Arguments.filters )>

  <cfset local.bSave = true>

  <cfcatch></cfcatch>
</cftry>

<cfif local.bSave>

</cfif>


有一篇较老的文章论述了这种模式。也许你也能从中找到一些灵感: