我有一个 CF 应用程序,它使用 jQuery 通过 $.post
调用 cfc。我在 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:\inetpub\wwwroot\mycfc.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)扩展。
例如,您可以使用 object.cfc 实现 onMissingMethod() ,然后您的所有 cfc 都可以扩展它。
同时,如果您希望处理 ajax 调用中的任何其他错误,那么您可以从 cfc 为任何 ajax 调用创建一个标准返回类型结构,并向其中添加一个状态字段。因此,当您在 cfc 中遇到任何错误时,您可以将状态设置为失败,而当一切正常时,您可以将状态设置为通过,稍后您可以在 ajax 返回函数中进行处理。
关于jquery - 通过 Ajax 调用 CFC 时捕获 CF 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19097586/