2

我有一个 ColdFusion 模板,它将通过 ajax 调用。该模板的超时时间为几秒,例如:

<cfsetting showdebugoutput="true" requesttimeout="2" />

但是如果发生超时,我不会收到任何错误消息。有没有办法通过 Ajax 捕捉超时并正确响应?

4

3 回答 3

3

我没有收到任何错误消息

你有什么要回来的吗?您的响应仍应包含 ColdFusion 生成的文本。您是否使用过 Chrome、fire bug 或任何带有网络工具的浏览器,这些工具可以让您查看响应以查看您是否只是找回了您无法使用的东西?您是否使用了任何可能抑制客户端失败的成功/失败回调函数?如果以下建议没有帮助,我会发布您的 JS 并重新标记您的问题。

话虽这么说:在请求超时到期后,任何时候你做任何事情,你都会想要延长你的超时时间。onError 和 try/catch 在超时后只有几个 MS 对错误做出反应,因此发送消息或记录错误也会失败。如果您尝试发送电子邮件或写入文件,那么您的 CF 错误消息会说您的 cfmail 或 cffile 操作已超时(技术上),但这不是导致初始错误的原因。

如果您知道超时时间,您可以简单地将 requestTimeout 重置为更大的值。

<cfsetting requesttimeout="2">
<cftry>
    <!--- some stuff that takes more than 2 seconds --->
    <cfcatch type = "any">
        <cfsetting requesttimeout="5">
        <!--- logging / error handling for timeout --->
        <!--- NOTE!! This does not add 5 seconds, it adds 3.  --->
        <!--- The value of requestTimeout is the total time of the timeout. --->
    </cfcatch>
</cftry>

或者,如果您不知道当前的超时值,您可以添加时间。

<cftry>
    <!--- some stuff that takes more than your timeout --->
    <cfcatch type = "any">
        <!--- You must first create your object to hold the requestMonitor. --->
        <cfset monitor = createObject("java", "coldfusion.runtime.RequestMonitor") />
        <!--- Then you need to reset your request timeout --->
        <!--- add 5 seconds to the timeout --->
        <cfsetting requesttimeout=monitor.getRequestTimeout()+5>
        <!--- logging / error handling for timeout --->
    </cfcatch>
</cftry>

您也不必在 try/catch 块中执行此操作,如果您在 application.cfc 中定义了一个,则可以将其包含在 onError 事件处理程序中。

于 2012-11-28T15:08:03.673 回答
0

如果 ColdFusion 模板位于具有 Application.cfc 文件的文件夹中,则您可以使用 onError 函数执行某些操作。

于 2012-11-28T12:42:35.783 回答
0

您应该将由 AJAX 调用的整个 ColdFusion 模板包装在一个cftry块中。这样,如果发生任何 ColdFusion 异常,您可以处理它并将您想要的任何消息返回给客户端。

<cftry>
    <!--- all of your ColdFusion template code here --->
    <cfcatch type="Any">
    <!--- return whatever you want here --->
        <div>An error has occurred.</div>
        <!--- remember that you have the cfcatch structure available to you --->
        <!--- which contains the specifics of the error that was thrown     --->
    </cfcatch>
</cftry>

在进行 AJAX 调用的客户端,您可以处理任何可能发生的与 http 连接相关的错误(连接失败、超时等)。您的 AJAX 调用将有一个错误方法或类似的方法。我强烈建议为此使用 jQuery。 jQuery AJAX API

您没有在帖子中提供任何具体信息。如果您遇到问题,实际分享您遇到问题的代码总是有帮助的。然后 StackOverflow 上的人可以提供更多帮助。

于 2012-11-28T13:43:14.350 回答