如何使用ASP在jQuery AJAX调用中触发“error”回调。净MVC吗?

时间:2022-11-29 13:44:27

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调用错误的问题有关。一些响应建议我使用“error”回调来显示jQuery AJAX调用中的任何错误。我想知道如何使用ASP。净MVC。是否有一种方法可以让我的控制器动作返回一个从“error”回调可以访问的错误?客户端代码是这样的:

$.ajax({
   type: "POST",
   url: "MyUrl",
   data: "val1=test",
   success: function(result){
        // Do stuff
   },
   error: function(request,status,errorThrown) {

   }
 });

6 个解决方案

#1


21  

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表示服务器端问题。

#2


8  

If you're using

如果您正在使用

[HandleError]

then throwing a HttpException is 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.

可能有一种更方便的写法,但我还没有发现。

#3


7  

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);
}

#4


1  

According to this page, you just need to apply the header HTTP/1.0 500 Internal Server Error on your ASP.net page. The $.ajax request will catch that error and execute the error callback function. =]

根据这个页面,您只需要在ASP.net页面上应用header HTTP/1.0 500内部服务器错误。美元的。ajax请求将捕获该错误并执行错误回调函数。=)

#5


1  

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");

#6


1  

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/

#1


21  

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表示服务器端问题。

#2


8  

If you're using

如果您正在使用

[HandleError]

then throwing a HttpException is 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.

可能有一种更方便的写法,但我还没有发现。

#3


7  

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);
}

#4


1  

According to this page, you just need to apply the header HTTP/1.0 500 Internal Server Error on your ASP.net page. The $.ajax request will catch that error and execute the error callback function. =]

根据这个页面,您只需要在ASP.net页面上应用header HTTP/1.0 500内部服务器错误。美元的。ajax请求将捕获该错误并执行错误回调函数。=)

#5


1  

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");

#6


1  

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/