我的客户群终于离开了Coldfusion 8,因此现在我可以利用Coldfusion 9的Application.cfc -> onCFCRequest事件。我有一个测试方案设置,但结果与预期不符。我有一个调用的方法,它会产生一个有效的XML响应,如下所示:

Response Header: Content-Type:application/xml;charset=UTF-8
Response:
<?xml version="1.0" encoding="UTF-8"?>
<rows><row id="10000282742505"><cell/><cell> ...

现在,在介绍了onCFCRequest事件之后,我将其恢复了(这打破了我的网格)...
Response Header: Content-Type:application/xml;charset=UTF-8
Response:
<wddxPacket version='1.0'><header/><data><string>&lt;rows&gt;&lt;row id="10000282742505"&gt;&lt;cell&gt;&lt;/cell&gt;&lt;cell&gt; ...

这是 Activity ...
<cffunction name="onCFCRequest" access="public" returntype="Any" output="true">
    <cfargument type="string" name="cfc" required="true">
    <cfargument type="string" name="method" required="true">
    <cfargument type="struct" name="args" required="true">

    <cfscript>
        // OnCFCRequest security hole fix as detailed here: http://blog.adamcameron.me/2013/04/its-easy-to-create-security-hole-in.html
        var o = createObject(ARGUMENTS.cfc);
        var metadata = getMetadata(o[ARGUMENTS.method]);

        if (structKeyExists(metadata, "access") && metadata.access == "remote"){
            return invoke(o, ARGUMENTS.method, ARGUMENTS.args);
        }else{
            throw(type="InvalidMethodException", message="Invalid method called", detail="The method #method# does not exists or is inaccessible remotely");
        }
    </cfscript>
    <cfreturn />
</cffunction>

如何获得onCFCRequest以与远程函数返回的格式相同的格式通过响应?

我知道这篇文章:http://www.bennadel.com/blog/1647-Learning-ColdFusion-9-Application-cfc-OnCFCRequest-Event-Handler-For-CFC-Requests.htm

我可能最终会尝试,但是首先我想弄清楚为什么我不能简单地以相同的格式传递响应。

最佳答案

我从没用过onCfcRequest,但是您说得对,有点傻。

似乎onCfcRequest也会“吞噬” returnFormat,因此您必须实现自己的returnFormat检测并将其序列化为正确的格式。



引用自:http://www.bennadel.com/blog/1647-Learning-ColdFusion-9-Application-cfc-OnCFCRequest-Event-Handler-For-CFC-Requests.htm

例如。

...
var result = invoke(o, ARGUMENTS.method, ARGUMENTS.args);
...
<!--- after your </cfscript> --->

<!--- TODO: do some checking to determine the preferred return type --->

<!--- if detected as xml, serve as xml (safer option) --->
<cfcontent type="application/xml"
           variable="#toBinay(toBase64(local.result))#">

<!--- *OR* (cleaner version) watch out for white spaces --->
<cfcontent type="application/xml">
<cfoutput>#result#</cfoutput>

09-11 22:50