从 cfc 抛出错误 - cflocation 在 onerror() 内部不起作用

发布于 2024-12-29 09:59:14 字数 3463 浏览 6 评论 0原文

编辑:一个重要的部分可能是我通过ajax调用cfc方法...

我通过ajax调用cfc方法,它返回在jqgrid表中使用的数据。在 cfc 的方法中,我尝试设置错误处理,以便将用户发送到一个错误页面,该页面在根目录中定义为 error.cfm,而不是仅仅失败并且不向用户提供任何问题的指示。 ,现在只包含一些样板 html。 在 Application.cfc 内部,我定义了 onError() 方法,如下所示:

<cffunction name="onError" returnType="void" output="false"  >
    <cfargument name="exception" required="true"  >
    <cfargument name="eventname" type="string" required="true"  >
    
    <cfmail to="#application.REGISTRATION_NOTIFICATION#" from="outgoing address" subject="Error">
    
    </cfmail>

    <cflocation url="../index.cfm?file=error.cfm" addToken="false"  /> 

当我从 cfc 的方法抛出错误时,无论是否在 cfcatch 内,Chrome 都会报告我确实已被发送到带有 STATUS= 的 index.cfm 200,并且响应正确显示了 error.cfm 模板内的 html。然而,浏览器实际上并没有进入该页面。

问题是,如果我将位置更改为

<cflocation url="index.cfm?file=error.cfm" addToken="false"  /> 

并从调用 cfc 的页面(而不是从 cfc)抛出错误,cflocation 确实会按预期工作。另外,如果我使用 cfinvoke 从原始页面调用 cfc,而不是使用 ajax 调用,则 cflocation 可以工作。所以这一定与我通过ajax调用cfc而不是从CF内部调用它有关。那么我在这里缺少什么?

ps 这种情况发生在 Chrome、IE 和 FF 中。我使用的是带有修补程序的 CF 9.0.1,没有框架。以下是 Chrome 网络窗格的输出子集,位于标题中,用于引用页面:

请求网址:http://localhost/sitename/cfc/method.cfc

请求方式:GET

状态代码:302 暂时移动

请求标头查看源代码

接受:application/json、text/javascript、/; q=0.01

接受字符集:ISO-8859-1,utf-8;q=0.7,*;q=0.3

接受编码:gzip、deflate、sdch

接受语言:en-US,en;q=0.8

连接:保持活动状态

Cookie:CFID=11801; CFTOKEN=90037336; LASTVISIT=1327852981759

主机:本地主机

引用者:http://localhost/sitename/index.cfm?file=referringfile.cfm

用户代理:Mozilla/5.0(Windows NT 6.1;WOW64)AppleWebKit/535.7 (KHTML,如 Gecko)Chrome/16.0.912.77 Safari/535.7

X-Requested-With:XMLHttpRequest

查询字符串参数查看编码的 URL

方法:获取零件编号

过滤器:{“page”:1,“sessionSQL”:“1327834969791”,“filterRows”:[{“FIELD_NAME”:“PART_NUMBER”,“OPERAND”:“EQUALS”,“FIELD_VALUE”:“11 06 01"}]}

_搜索:假

nd:1327835014639

行数:100

页数:1

sidx:PART_NUMBER

排序:描述

响应标头查看源代码

内容类型:text/html;字符集=UTF-8

日期:2012 年 1 月 29 日星期日 11:03:34 GMT

位置:../index.cfm?file=error.cfm

持久验证:true

服务器:Microsoft-IIS/7.5

Set-Cookie:LASTVISIT=1327853014657;expires=2012 年 7 月 27 日星期五 11:03:34 GMT;路径=/

传输编码:分块

X-Powered-By:ASP.NET

对于 error.cfm 页面:

请求网址:http://localhost/sitename/index.cfm?file=error.cfm

请求方式:GET

状态代码:200 OK

请求标头查看源代码

接受:application/json、text/javascript、/; q=0.01

接受字符集:ISO-8859-1,utf-8;q=0.7,*;q=0.3

接受编码:gzip、deflate、sdch

接受语言:en-US,en;q=0.8

授权:协商 巴拉巴拉巴拉巴拉巴拉巴拉巴拉巴拉巴拉巴拉巴拉巴拉巴拉巴拉巴拉巴拉巴拉巴拉巴拉巴拉巴拉巴拉巴拉巴拉巴拉巴拉巴拉巴拉巴拉巴拉巴拉巴拉巴拉巴拉巴拉巴拉巴拉巴拉巴拉巴拉巴拉巴拉巴拉巴拉巴拉巴拉巴拉巴拉巴拉巴拉等等

连接:保持活动状态

Cookie:CFID=11801; CFTOKEN=90037336; LASTVISIT=1327716554490

主机:本地主机

引用者:http://localhost/sitename/index.cfm?file=filename/filename.cfm&Config=filename

用户代理:Mozilla/5.0(Windows NT 6.1;WOW64)AppleWebKit/535.7(KHTML,如 Gecko)Chrome/16.0.912.77 Safari/535.7

X-Requested-With:XMLHttpRequest

查询字符串参数查看 URL 编码

文件:error.cfm

响应标头查看源代码

内容类型:text/html; charset=UTF-8 日期:2012 年 1 月 27 日星期五 21:09:14

GMT 持久验证:true 服务器:Microsoft-IIS/7.5

Set-Cookie:LASTVISIT=1327716554533;expires=星期三,2012 年 7 月 25 日 21:09:14 GMT;path=/

传输编码:分块

X-Powered-By:ASP.NET

Edit: an important piece might be that I'm calling a cfc method via ajax...

I'm calling a cfc method via ajax, which returns data for use in a jqgrid table. Inside the cfc's method, I'm trying to set up error handling, so that instead of just failing and giving the user no indication of any problems, I send users to an error page, which is defined as error.cfm in the root directory, and which right now just includes some boilerplate html.
Inside Application.cfc, I've defined my onError() method like so:

<cffunction name="onError" returnType="void" output="false"  >
    <cfargument name="exception" required="true"  >
    <cfargument name="eventname" type="string" required="true"  >
    
    <cfmail to="#application.REGISTRATION_NOTIFICATION#" from="outgoing address" subject="Error">
    
    </cfmail>

    <cflocation url="../index.cfm?file=error.cfm" addToken="false"  /> 

When I throw an error from the cfc's method, whether inside a cfcatch or not, Chrome reports that I have indeed been sent to index.cfm with a STATUS=200, and the response shows, correctly, the html that is inside the error.cfm template. However, the browser does not actually go to the page.

The thing is, if I change the location to

<cflocation url="index.cfm?file=error.cfm" addToken="false"  /> 

and throw an error from the page which was calling the cfc, instead of from the cfc, cflocation does work as expected. Also, if I use cfinvoke to call the cfc from the original page, instead of using the ajax call, cflocation works. So this must have something to do with the fact that I'm making the call to the cfc through ajax, instead of invoking it from within CF. So what am I missing here?

p.s. this happens in Chrome, IE, and FF. I'm using CF 9.0.1 with hotfixes, no frameworks. Here's a subset of the output from Chrome's Network pane, in Headers, for the referring page:

Request URL:http://localhost/sitename/cfc/method.cfc

Request Method:GET

Status Code:302 Moved Temporarily

Request Headers view source

Accept:application/json, text/javascript, /; q=0.01

Accept-Charset:ISO-8859-1,utf-8;q=0.7,*;q=0.3

Accept-Encoding:gzip,deflate,sdch

Accept-Language:en-US,en;q=0.8

Connection:keep-alive

Cookie:CFID=11801; CFTOKEN=90037336; LASTVISIT=1327852981759

Host:localhost

Referer:http://localhost/sitename/index.cfm?file=referringfile.cfm

User-Agent:Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.7
(KHTML, like Gecko) Chrome/16.0.912.77 Safari/535.7

X-Requested-With:XMLHttpRequest

Query String Parameters view URL encoded

method:getPartNumbers

filter:{"page":1,"sessionSQL":"1327834969791","filterRows":[{"FIELD_NAME":"PART_NUMBER","OPERAND":"EQUALS","FIELD_VALUE":"11
06 01"}]}

_search:false

nd:1327835014639

rows:100

page:1

sidx:PART_NUMBER

sord:desc

Response Headers view source

Content-Type:text/html; charset=UTF-8

Date:Sun, 29 Jan 2012 11:03:34 GMT

Location:../index.cfm?file=error.cfm

Persistent-Auth:true

Server:Microsoft-IIS/7.5

Set-Cookie:LASTVISIT=1327853014657;expires=Fri, 27-Jul-2012 11:03:34
GMT;path=/

Transfer-Encoding:chunked

X-Powered-By:ASP.NET

And for the error.cfm page:

Request URL:http://localhost/sitename/index.cfm?file=error.cfm

Request Method:GET

Status Code:200 OK

Request Headers view source

Accept:application/json, text/javascript, /; q=0.01

Accept-Charset:ISO-8859-1,utf-8;q=0.7,*;q=0.3

Accept-Encoding:gzip,deflate,sdch

Accept-Language:en-US,en;q=0.8

Authorization:Negotiate
blahblahblahblah

Connection:keep-alive

Cookie:CFID=11801; CFTOKEN=90037336; LASTVISIT=1327716554490

Host:localhost

Referer:http://localhost/sitename/index.cfm?file=filename/filename.cfm&Config=filename

User-Agent:Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.7 (KHTML, like Gecko) Chrome/16.0.912.77 Safari/535.7

X-Requested-With:XMLHttpRequest

Query String Parameters view URL encoded

file:error.cfm

Response Headers view source

Content-Type:text/html; charset=UTF-8 Date:Fri, 27 Jan 2012 21:09:14

GMT Persistent-Auth:true Server:Microsoft-IIS/7.5

Set-Cookie:LASTVISIT=1327716554533;expires=Wed, 25-Jul-2012 21:09:14 GMT;path=/

Transfer-Encoding:chunked

X-Powered-By:ASP.NET

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

陌伤ぢ 2025-01-05 09:59:14

编辑:抱歉,误读了问题...

更新:您可以尝试包含一个模板来处理 cflocation,在 application.cfc 内执行 cflocation 可能会被禁止,因为它发生在请求被触发之前?

Application.cfc

 component {

    this.name = 'errorTest';

    public void function onError(required any Exception, required string EventName) {

        include 'errorRedirect.cfm';
    }
}

index.cfm(演示代码中有错误)

<cfscript>
a = form.nonExistentVariable;
</cfscript>

errorRedirect.cfm

<cflocation url="error.html">

error.html

<h1>Sorry, An Error has Occurred. </h1>

test.cfc(使用 test.cfc?method=test 测试基于演示 cfc 的错误)

component {

remote function test () {

    a = form.DoesNotExist;
}
}

Edit: Sorry, misread the question...

UPDATED: You could try including a template to handle the cflocation, doing a cflocation inside the application.cfc might be disallowed since its happening before the request is fired?

Application.cfc

 component {

    this.name = 'errorTest';

    public void function onError(required any Exception, required string EventName) {

        include 'errorRedirect.cfm';
    }
}

index.cfm (has an error in the code for demo)

<cfscript>
a = form.nonExistentVariable;
</cfscript>

errorRedirect.cfm

<cflocation url="error.html">

error.html

<h1>Sorry, An Error has Occurred. </h1>

test.cfc (test with test.cfc?method=test to demo cfc based error)

component {

remote function test () {

    a = form.DoesNotExist;
}
}
灼痛 2025-01-05 09:59:14

如果您使用 AJAX,则除非使用 CF 绑定到 HTML 文档中的 HTML 元素,否则将不会显示结果输出。

当您发出 AJAX 请求时,您可以使用 chrome 看到错误页面的响应正文(实体)中的任何内容吗?

我认为这是蓝龙应用程序服务器?

If you use AJAX then the resulting output will not display unless bound to an HTML element in the HTML document using CF.

Can you see any content in the response body (the entity) for error page using chrome when you make the AJAX request?

I take it this is a Bluedragon application server?

飘逸的'云 2025-01-05 09:59:14

我想我在这里得到了答案:

使用 Application.cfc 的 OnError 事件方法处理远程 API 错误

我猜我知道的 Ajax 很危险:(

我认为让我感到困惑的是我可以看到 cflocation 确实重定向到错误页面,并且我可以在网络窗格中看到正确的响应,但我没有意识到,由于我远程调用该函数,浏览器实际上永远不会转到新窗口; ,相反,响应坐在那里等待 ajax 调用本身处理,我最终会解决这个问题。

I think I've got my answer here:

Handling Remote API Errors With Application.cfc's OnError Event Method

Guess I know just enough Ajax to be dangerous :(

I think what threw me is that I could see that cflocation was indeed redirecting to the error pages and that I could see the correct response in the network pane; but what I didn't realize is that since I was calling the function remotely, the browser will never actually go to the new window, and instead, the response is sitting there waiting to be handled by the ajax call itself. I'll figure this stuff out eventually.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文