2013-11-27 154 views
0

这个动作被一个ajax调用。我想知道如何在excepion中捕获消息。如何在Ajax请求未成功时显示错误消息?

[HttpPost] 
    [Authorize] 
    public ActionResult AjaxManage(ManageClassification model, string random) 
    { 
     if (model.Classification.ClassificationId == 0 && 
      !string.IsNullOrEmpty(model.Classification.ClassificationName)) 
     { 
      // 
     } 
     else 
     { 
      throw new Exception("Entity cannot be empty."); 
     } 
    } 

如何将错误显示回页面?

$.ajax({ 
      //... 
      , 
      error:function(){ 
       //display error here ... 
      } 
     }); 

如何捕获并显示发送的错误消息是位于服务器中的操作?一旦捕获到消息,它就会显示给用户,看看出了什么问题。

感谢您的帮助。

回答

0

您可以按照这里的步骤来显示引发的错误。这是ASP.NET,但应适用于MVC

http://encosia.com/use-jquery-to-catch-and-display-aspnet-ajax-service-errors/

您也可能会在浏览器控制台

+0

'VAR ERR =的eval(” (“+ xhr.responseText +”)“);'和'alert(err.Message);'抛出错误。 – Richard77

+0

它抛出了什么错误? – Sico

+0

它不起作用,即它到达代码的那一部分时没有响应。我不知道发生了什么错误。 – Richard77

0

尝试一些调试信息,

error : function(XMLHttpRequest, textStatus, errorThrown) 
{ 
    alert(errorThrown); 
    alert(textStatus); 
} 
+0

** textStatus **正显示文本'error',而** errorThrown **正在显示文本'undifined' – Richard77

相关问题