你如何触发"错误"回调jQuery的AJAX调用使用ASP.NET MVC? [英] How do you trigger the "error" callback in a jQuery AJAX call using ASP.NET MVC?

查看:120
本文介绍了你如何触发"错误"回调jQuery的AJAX调用使用ASP.NET MVC?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是有关我的问题在如何从jQuery的处理错误AJAX调用。一些答复建议我用错误回调来显示一个jQuery AJAX调用任何错误。我不知道该怎么使用ASP.NET MVC的事情。有没有办法为我的控制器行动来回报,这将是从错误回调访问的错误?客户端code会是这个样子:

  $。阿贾克斯({
   键入:POST,
   网址:MyUrl
   数据:VAL1 =测试,
   成功:函数(结果){
        //做的东西
   },
   错误:函数(请求,状态,errorThrown){

   }
 });
 

解决方案

注:嘿,这是之前发布ASP.Net MVC连打1.0,从那以后,我还没有看的框架。你或许应该停止upvoting这一点。


做这样的事情:

  Response.Status code =(INT)的HTTPStatus code.BadRequest;
的ActionResult = this.Content(在此错误消息);
 

根据错误的性质状态code应该改变;通常,4XX用户生成的问题和5XX服务器端的问题。

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:

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

   }
 });

解决方案

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.


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.

这篇关于你如何触发"错误"回调jQuery的AJAX调用使用ASP.NET MVC?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆