【发布时间】:2014-10-02 21:59:19
【问题描述】:
我在 jquery 中调用的 aspx 页面上有一个 WebMethod,我试图让它在弹出框中显示引发异常的消息,但不是在错误函数下运行代码,而是在调试器停止说“用户未处理的异常”。如何将错误返回给客户端?
[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public static void SubmitSections(string item)
{
try
{
throw new Exception("Hello");
}
catch (Exception ex)
{
HttpContext.Current.Response.Write(ex.Message);
throw new Exception(ex.Message, ex.InnerException);
}
}
在我的 js 文件中:
$.ajax({
type: "POST",
url: loc + "/SubmitSections",
data: dataValue,
contentType: 'application/json; charset=utf-8',
dataType: 'json',
success: function (Result) {
$("#modal-submitting").modal('hide');
document.location = nextPage;
},
error: function (XMLHttpRequest, textStatus, errorThrown) {
$("#modal-submitting").modal('hide');
alert("Request: " + XMLHttpRequest.toString() + "\n\nStatus: " + textStatus + "\n\nError: " + errorThrown);
}
});//ajax call end
【问题讨论】:
-
查看这篇文章作为如何阅读抛出的异常的解决方案:stackoverflow.com/a/70009394/2668852
标签: javascript c# jquery asp.net ajax