如何在使用 ASP.NET MVC 的 jQuery AJAX 调用中触发“错误”回调?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/407651/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do you trigger the "error" callback in a jQuery AJAX call using ASP.NET MVC?
提问by Kevin Pang
This is related to my question on how to handle errors from jQuery AJAX calls. Several responses suggested that I use the "error" callback to display any errors from a jQuery AJAX call. I was wondering how to do that using ASP.NET MVC. Is there a way for my controller action to return an error that would be accessible from the "error" callback? The client side code would look something like this:
这与我关于如何处理来自 jQuery AJAX 调用的错误的问题有关。一些回复建议我使用“错误”回调来显示来自 jQuery AJAX 调用的任何错误。我想知道如何使用 ASP.NET MVC 做到这一点。有没有办法让我的控制器操作返回可从“错误”回调访问的错误?客户端代码如下所示:
$.ajax({
type: "POST",
url: "MyUrl",
data: "val1=test",
success: function(result){
// Do stuff
},
error: function(request,status,errorThrown) {
}
});
采纳答案by Adam Lassek
NOTE: Hey, this was posted before ASP.Net MVC even hit 1.0, and I haven't even looked at the framework since then. You should probably stop upvoting this.
注意:嘿,这是在 ASP.Net MVC 甚至达到 1.0 之前发布的,从那时起我什至没有看过框架。您可能应该停止对此进行投票。
Do something like this:
做这样的事情:
Response.StatusCode = (int)HttpStatusCode.BadRequest;
actionResult = this.Content("Error message here");
The status code should change depending on the nature of the error; generally, 4xx for user-generated problems and 5xx for server-side problems.
状态代码应该根据错误的性质而改变;通常,用户生成的问题为 4xx,服务器端问题为 5xx。
回答by Todd Smith
If you're using
如果您正在使用
[HandleError]
then throwing a HttpExceptionis going to get caught and routed to your custom error page.
然后抛出HttpException将被捕获并路由到您的自定义错误页面。
Another option is to use
另一种选择是使用
Response.StatusCode = 500;
Response.Write("Error Message");
Response.End();
There's probably a more convenient way to write this but I haven't stumbled upon it yet.
可能有一种更方便的方法来写这个,但我还没有偶然发现它。
回答by Derek Morrison
If you're using MVC 3, then you can return an ActionResult in your controller that has the HTTP status code and status message together:
如果您使用的是 MVC 3,那么您可以在控制器中返回一个 ActionResult,其中包含 HTTP 状态代码和状态消息:
return new HttpStatusCodeResult(500, "Error message");
Then, in your error callback:
然后,在您的错误回调中:
error: function (request, textStatus, errorThrown) {
alert(request.statusText);
}
回答by Rodrigo Perez Burgues
I send you a proposal; works with contrelled and uncontrolled exceptions.
我给你一个建议;适用于受控和不受控制的异常。
public class CodeExceptionToHttpFilter : FilterAttribute, IExceptionFilter
{
public CodeExceptionToHttpFilter()
{
Order = 2;
}
public void OnException(ExceptionContext filterContext)
{
var codeException = filterContext.Exception as CodeException;
var response = filterContext.RequestContext.HttpContext.Response;
response.StatusCode = (codeException == null)? 550: 551;
response.ContentType = MediaTypeNames.Text.Plain;
response.Charset = "utf-8";
response.Write(filter.Exception.Message);
filterContext.ExceptionHandled = true;
response.TrySkipIisCustomErrors = true;
}
}
}
More info on my blog. http://rodrigopb.wordpress.com/2012/11/28/gestion-de-errores-en-peticiones-ajax-a-mvc/
更多信息在我的博客上。 http://rodrigopb.wordpress.com/2012/11/28/gestion-de-errores-en-peticiones-ajax-a-mvc/
回答by John Sheehan
I think that event is raised for any response that has a response code other than 200. I can't find proof of this in the docs though.
我认为该事件是针对响应代码不是 200 的任何响应引发的。不过,我在文档中找不到这方面的证据。
To do this from code (works in Webforms):
要从代码中执行此操作(适用于 Webforms):
throw new HttpException(500, "Error message");