通过 Ajax 调用 CFC 时捕获 CF 错误



我有一个使用jQuery通过$.post调用cfc的CF应用程序。 我在 cfc 中有错误处理,但是捕获此示例呢:

氟氯化碳:

<cfcomponent>
    <cffunction name="function_1" access="remote" returnformat="json">
        ...
    </cffunction>
    ....
</cfcomponent>

呼叫页面:

<html>
    <head>...</head>
    <body>...
        <script>
            $.post("mycfc.cfc",{
                method: "functio_1", //notice the misspelling
                ....
        </script>
    </body>
</html>

这不会导致 javascript 错误,并返回 200 响应。

错误将是:

The method functio_1 was not found in component E:inetpubwwwrootmycfc.cfc. Ensure that the method is defined, and that it is spelled correctly. <br>The error occurred on line -1. 

我知道这是基本示例,并且很容易修复,但我相信还有很多其他示例可能会发生类似的事情。

有什么想法吗?

要处理缺失函数,你可以看看 Ben Nadel 对 onMissingMethod() 函数的解释 http://www.bennadel.com/blog/868-Learning-ColdFusion-8-OnMissingMethod-Event-Handler.htm

您始终可以将此函数放在父类中,该父类可以通过所有子类(即 cfc)进行扩展。

例如,你可以用这个onMissingMethod()实现来使用object.cfc,然后所有的cfc都可以扩展它。

同时,如果您希望处理 ajax 调用中的任何其他错误,则可以从 cfc 为任何 ajax 调用创建一个标准返回类型结构,并向其添加状态字段。因此,当您在 cfc 中收到任何错误时,您可以将状态设置为失败,当一切正常时,您可以将状态设置为通过,稍后可以在 ajax 返回函数中由您处理。

最新更新