gpt4 book ai didi

jquery - 在 ASP.NET MVC 中显示 Json 错误消息

转载 作者:行者123 更新时间:2023-12-01 01:16:40 25 4
gpt4 key购买 nike

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

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

Controller :

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 错误消息。像显示警报消息框警报(“测试错误”)这样简单的事情。

有什么办法可以让事情变得更简单吗?

我在 asp.net 上看到了这个链接,看起来不错 How Do I Handle jQuery Errors and Display them on the Client?

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

最佳答案

在你的捕获中,你可能应该记录错误,然后返回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");

上面抛出的异常将返回完整的错误 View 。

关于jquery - 在 ASP.NET MVC 中显示 Json 错误消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13413218/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com