3

我正在创建我的第一个 ASP.NET MVC2 项目,在客户端使用 jquery 和 ajax。

现在,当我进行 QA 测试应用程序并添加一些测试异常并将其从我的控制器传递给 json 时,如下所示:

控制器:

public JsonResult PopulateDetails(int id)
        {
                  ProductServiceClient client = new ProductServiceClient();
            try
            {
                if (id == 0)
                { throw new Exception("TEST ERROR."); }

                string name= "test";
                List<Product> product= client.GetPriductbyName(name);
                return Json(product);

            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

Jquery-Ajax 脚本:

$.ajax({
        type: "POST",
        url: url_ ,
        data: search,
        success: function(data) {   // data: Passed (Records) from the PopulateDetails controller
            displayDetails(data);   // after routing on Populate Details  converts the data to table
        },
        error: function(XMLHttpRequest, textStatus, errorThrown) {
            alert("error: " + XMLHttpRequest.responseText);
        },
        dataType: 'json'
    });

我注意到它将在下面返回此默认错误消息:

---------------------------
Windows Internet Explorer
---------------------------
error: <html>

    <head>

        <title>TEST ERROR.</title>

        <style>

         body {font-family:"Verdana";font-weight:normal;font-size: .7em;color:black;} 

         p {font-family:"Verdana";font-weight:normal;color:black;margin-top: -5px}

         b {font-family:"Verdana";font-weight:bold;color:black;margin-top: -5px}

         H1 { font-family:"Verdana";font-weight:normal;font-size:18pt;color:red }

         H2 { font-family:"Verdana";font-weight:normal;font-size:14pt;color:maroon }

         pre {font-family:"Lucida Console";font-size: .9em}

         .marker {font-weight: bold; color: black;text-decoration: none;}

         .version {color: gray;}

         .error {margin-bottom: 10px;}

         .expandable { text-decoration:underline; font-weight:bold; color:navy; cursor:hand; }

        </style>

    </head>



    <body bgcolor="white">



            <span><H1>Server Error in '/' Application.<hr width=100% size=1 color=silver></H1>



            <h2> <i>TEST ERROR.</i> </h2></span>



            <font face="Arial, Helvetica, Geneva, SunSans-Regular, sans-serif ">



            <b> Description: </b>An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.



            <br><br>



            <b> Exception Details: </b>System.Exception: TEST ERROR.<br><br>



            <b>Source Error:</b> <br><br>



            <table width=100% bgcolor="#ffffcc">

               <tr>

                  <td>

                      <code><pre>



Line 64:             catch (Exception ex)

Line 65:             {

<font color=red>Line 66:                 throw ex;

</font>Line 67:             }

Line 68:         }</pre></code>



                  </td>

               </tr>

            </table>



            <br>

---------------------------
OK   
---------------------------

现在如何整齐地显示异常中的错误消息而不是整个 html 错误消息。类似于显示警告消息框 alert("TEST ERROR") 之类的简单操作。

有没有办法让它更简单?

我在 asp.net 上看到了这个链接,看起来不错如何处理 jQuery 错误并在客户端上显示它们?

但我找不到 AjaxResponseViewModel() 的声明位置。

4

1 回答 1

3

在您的捕获中,您可能应该记录错误,然后返回一个json 友好的结果,例如

 catch (Exception ex)
 {
      //log.Error(ex); using whatever you use to log
      return Json(ex.Message);
 }

或者

return Json("There has been an error, please contact support or read the log");

上面你抛出了异常,它将完整地返回错误视图。

于 2012-11-16T09:08:09.213 回答