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

Rob*_*b M 2 ajax coldfusion jquery

我有一个使用jQuery来调用cfc的CF应用程序$.post.我在cfc中有错误处理,但是如何捕获这个例子:

CFC:

<cfcomponent>
    <cffunction name="function_1" access="remote" returnformat="json">
        ...
    </cffunction>
    ....
</cfcomponent>
Run Code Online (Sandbox Code Playgroud)

呼叫页面:

<html>
    <head>...</head>
    <body>...
        <script>
            $.post("mycfc.cfc",{
                method: "functio_1", //notice the misspelling
                ....
        </script>
    </body>
</html>
Run Code Online (Sandbox Code Playgroud)

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

错误将是:

The method functio_1 was not found in component E:\inetpub\wwwroot\mycfc.cfc. Ensure that the method is defined, and that it is spelled correctly. <br>The error occurred on line -1. 
Run Code Online (Sandbox Code Playgroud)

我知道这是一个基本的例子,很容易修复,但我确信还有很多其他例子可能会发生类似的事情.

有什么想法吗?

nas*_*saa 6

要处理缺失的函数,你可以看看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中收到任何错误时,您可以将状态设置为失败,当事情正常时,您可以将状态设置为pass,稍后您可以在ajax return函数中处理.