无论我做什么都无法命中 OnError
已解决,见下文
在我的控制器中...
[HttpPost]
public ActionResult Reject(RevisionRejectModel model)
{
throw new HttpException(500, "Internal Error");
...
}
在我的视图中...
@using (Ajax.BeginForm("Reject", "Revision", new AjaxOptions
{
HttpMethod = "Post",
OnBegin = "RejectOnBegin",
OnFailure = "RejectOnFailure",
OnSuccess = "RejectOnSuccess",
OnComplete = "RejectOnComplete"
}))
...
在我的.js...
function RejectOnFailure(ajaxContext) {
alert("Never hit!");
}
已解决
问题是在中打开了自定义错误网络配置... 即使在 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"
}
SOLVED, see below
In my Controller...
[HttpPost]
public ActionResult Reject(RevisionRejectModel model)
{
throw new HttpException(500, "Internal Error");
...
}
In my View...
@using (Ajax.BeginForm("Reject", "Revision", new AjaxOptions
{
HttpMethod = "Post",
OnBegin = "RejectOnBegin",
OnFailure = "RejectOnFailure",
OnSuccess = "RejectOnSuccess",
OnComplete = "RejectOnComplete"
}))
...
In my .js...
function RejectOnFailure(ajaxContext) {
alert("Never hit!");
}
SOLVED
The problem was that customerrors was turned on in web.config...
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我假设这里有一个自定义错误页面,将您重定向到错误控制器。情况可能并非如此,但首先验证 - 加载 fiddler 并在浏览器中运行对 /Revision/Reject 的请求,并查看响应代码是否为 500。
Im going to assume here you have a custom error page redirecting you to an error controller. It may not be the case, but first verify - load up fiddler and run the request in the browser to /Revision/Reject and see if the response code is 500.