看来我不能简单地通过相同格式的结果,因为这个......”默认情况下,ColdFusion 将除 XML 之外的所有返回类型(包括简单返回类型)序列化为 WDDX 格式,并将 XML 数据作为 XML 返回文本。 ”(http://help.adobe.com/en_US/ColdFusion/9.0/CFMLRef/WSc3ff6d0ea77859461172e0811cbec22c24-7f5c.html)。被调用的远程函数将 myxml
作为字符串返回给onCFCRequest
,onCFCRequest
然后将该简单返回类型(此时为字符串)转换为WDDX
,因为这是默认行为。
所以...
经过大量测试后,我最终采用了 Ben Nadel 文章中的解决方案,但我想提一下一些调整。
- 第一个添加是添加我的问题中已经显示的代码,以解决 Adam Cameron 发现的错误。
- 第二个添加是在调用之后立即执行此操作:
<cfset local.result = ToString(local.result)>
。在 Ben 的评论中,它说“ ......所有返回的值都将是字符串...... ”但情况似乎并非如此。我们有一些只返回数字的远程函数。如果没有ToString()
将响应转换为二进制的代码,则会失败。
- 在设置 mimeTypes 的部分中,我将 IF 语句更改为
json
. 在我们编写的每个远程函数中,我们创建一个 ColdFusion 结构,然后像这样返回它<cfreturn SerializeJSON(z.response) />
:这似乎比手动拼凑一个 json 字符串,然后在onCFCRequest
. 因此,在 json mimeType 的 onCFCRequest 中,我只是将其视为字符串,因为它已经序列化了;所以不需要第二次序列化它。
- 同样在 mimeType 部分,我为
xml
. 我们有许多远程函数可以输出xml
网格,而不是wddx
. 而且由于没有returnFormat
ofxml
我在支票上方添加了returnType
支票。xml
wddx
- 根据@Henry的评论将 responseMimeType 更改为和
JSON
更改。谢谢!XML
application/json
application/xml
非常感谢 Ben 和 Adam 奠定了基础!
这是最终的结果...
<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." />
<!---
Here we can setup any request level variables we want
and they will be accessible to all remote cfc calls.
--->
<cfset request.jspath = 'javascript'>
<cfset request.imgpath = 'images'>
<cfset request.csspath = 'css'>
<!---
Check to see if the target CFC exists in our cache.
If it doesn't then, create it and cached it.
--->
<cfif !structKeyExists( application.apiCache, arguments.component )>
<!---
Create the CFC and cache it via its path in the
application cache. This way, it will exist for
the life of the application.
--->
<cfset application.apiCache[ arguments.component ] = createObject( "component", arguments.component ) />
</cfif>
<!---
ASSERT: At this point, we know that the target
component has been created and cached in the
application.
--->
<!--- Get the target component out of the cache. --->
<cfset local.cfc = application.apiCache[ arguments.component ] />
<!--- Get the cfcs metaData --->
<cfset var metadata = getMetaData( local.cfc[ arguments.methodName ] )>
<!--- OnCFCRequest security hole fix as detailed here: http://cfmlblog.adamcameron.me/2013/04/its-easy-to-create-security-hole-in.html --->
<cfif structKeyExists(metadata, "access") and metadata.access eq "remote">
<!--- Good to go! --->
<cfelse>
<cfthrow type="InvalidMethodException" message="Invalid method called" detail="The method #arguments.methodName# does not exists or is inaccessible remotely">
</cfif>
<!---
Execute the remote method call and store the response
(note that if the response is void, it will destroy
the return variable).
--->
<cfinvoke returnvariable="local.result" component="#local.cfc#" method="#arguments.methodName#" argumentcollection="#arguments.methodArguments#" />
<!---
We have some functions that return only a number (ex: lpitems.cfc->get_lpno_onhandqty).
For those we must convert the number to a string, otherwise, when we try to
convert the response to binary down at the bottom of this function it will bomb.
--->
<cfset local.result = ToString(local.result)>
<!---
Create a default response data variable and mime-type.
While all the values returned will be string, the
string might represent different data structures.
--->
<cfset local.responseData = "" />
<cfset local.responseMimeType = "text/plain" />
<!---
Check to see if the method call above resulted in any
return value. If it didn't, then we can just use the
default response value and mime type.
--->
<cfif structKeyExists( local, "result" )>
<!---
Check to see what kind of return format we need to
use in our transformation. Keep in mind that the
URL-based return format takes precedence. As such,
we're actually going to PARAM the URL-based format
with the default in the function. This will make
our logic much easier to follow.
NOTE: This expects the returnFormat to be defined
on your CFC - a "best practice" with remote
method definitions.
--->
<cfparam name="url.returnFormat" type="string" default="#metadata.returnFormat#" />
<cfparam name="url.returnType" type="string" default="#metadata.returnType#" /> <!--- Added this line so we can check for returnType of xml --->
<!---
Now that we know the URL scope will have the
correct format, we can check that exclusively.
--->
<cfif (url.returnFormat eq "json")>
<!--- Convert the result to json. --->
<!---
We already serializeJSON in the function being called by the user, this would cause double encoding, so just treat as text
<cfset local.responseData = serializeJSON( local.result ) />
--->
<cfset local.responseData = local.result />
<!--- Set the appropriate mime type. --->
<cfset local.responseMimeType = "application/json" />
<!---
There is no returnFormat of xml so we will check returnType instead.
This leaves the door open for us to use wddx in future if we decide to.
--->
<cfelseif (url.returnType eq "xml")>
<!--- Convert the result to string. --->
<cfset local.responseData = local.result />
<!--- Set the appropriate mime type. --->
<cfset local.responseMimeType = "application/xml" />
<cfelseif (url.returnFormat eq "wddx")>
<!--- Convert the result to XML. --->
<cfwddx action="cfml2wddx" input="#local.result#" output="local.responseData" />
<!--- Set the appropriate mime type. --->
<cfset local.responseMimeType = "application/xml" />
<cfelse>
<!--- Convert the result to string. --->
<cfset local.responseData = local.result />
<!--- Set the appropriate mime type. --->
<cfset local.responseMimeType = "text/plain" />
</cfif>
</cfif>
<!---
Now that we have our response data and mime type
variables defined, we can stream the response back
to the client.
--->
<!--- Convert the response to binary. --->
<cfset local.binaryResponse = toBinary( toBase64( local.responseData ) ) />
<!---
Set the content length (to help the client know how
much data is coming back).
--->
<cfheader name="content-length" value="#arrayLen( local.binaryResponse )#" />
<!--- Stream the content. --->
<cfcontent type="#local.responseMimeType#" variable="#local.binaryResponse#" />
<!--- Return out. --->
<cfreturn />
</cffunction>