Ajax.BeginForm OnFailure 永远不会被调用
我在视图中使用 Ajax.BeginForm,并在 .js 中提供 OnFailure 处理程序,但它永远不会被调用。我尝试在控制器中抛出异常(抛出 new HttpException(500, "Internal Error")),但它永远不会被调用。 原因似乎是当我在 web.config 中打开了自定义错误时。我想要做的是处理我的控制器中的任何异常或错误,并将它们传递给它们沿着我的 .js 文件中的 OnFailure 处理程序,就好像自定义错误已关闭一样。有什么办法可以做到这一点吗?
已解决:
即使在 web.config 中打开自定义错误也能正常工作...
我处理此问题的方法是设置 Response.StatusCode = 500 并将标头附加到我的响应对象 Response.AppendHeader("MyResponseHeader", "My Message");
在我的 .js OnFailure 处理程序中...
function OnFailure(ajaxContext) {
alert(ajaxContext.status); // 500
alert(ajaxContext.getResponseHeader("MyResponseHeader")); // "My Message"
}
I am using Ajax.BeginForm in my View, and providing an OnFailure handler in my .js, but it never gets called. I've tried throwing an exception (throw new HttpException(500, "Internal Error")) in my controller and it never gets called. The reason seems to be when I have customerrors turned on in the web.config. What I want to do, is handle any exceptions or errors in my controller, and pass them along to my the OnFailure handler in my .js file as if customerrors were turned off. Is there any way to do this?
SOLVED:
Works even with customerrors turned on in web.config...
The way I handle this, is to set the Response.StatusCode = 500 and append a Header to my response object Response.AppendHeader("MyResponseHeader", "My Message");
In my .js OnFailure handler...
function OnFailure(ajaxContext) {
alert(ajaxContext.status); // 500
alert(ajaxContext.getResponseHeader("MyResponseHeader")); // "My Message"
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论