【发布时间】:2017-06-23 23:08:00
【问题描述】:
我正在使用 jQuery 的 ajax 从客户端调用控制器方法。
$.ajax({
url: "/Services/VendorServices.asmx/SendVendorRequestNotifications",
type: 'POST',
contentType: "application/json",
dataType: "json",
data: JSON.stringify({ vendorModel: info }),
timeout: requestTimeOut,
success: function (data) {
// Success
},
error: function (jqXHR, textStatus, errorThrown) {
// Error: Would like to display meaningful message here!
}
});
如果我的SendVendorRequestNotifications 抛出异常,我想在客户端显示一条错误消息。但是,我似乎无法向用户显示一条好消息。
文本状态:
错误
errorThrown:
内部服务器错误
jqXHR.responseText:
{"Message":"发生一个或多个错误。","StackTrace":" at System.Threading.Tasks.Task.ThrowIfExceptional(Boolean includeTaskCanceledExceptions)
在 System.Threading.Tasks.Task'1.GetResultCore(Boolean waitCompletionNotification)
在 System.Threading.Tasks.Task'1.get_Result()","ExceptionType":"System.AggregateException"}
如您所见,这些都不是有用的数据。我希望能够在服务器上收集更有意义的消息,并将其提供给$.ajax 调用的error 处理程序。
这可能吗?
【问题讨论】:
-
是的。看看这个。你能显示控制器代码吗?可能会有所帮助。 stackoverflow.com/questions/4707755/…
-
@mjw:我的实际控制人有很多事情要做。不知道会感兴趣。主要是我有一个
try...catch块,我的catch块记录错误然后重新抛出异常。 -
你能显示存根吗?捕获块?这些可能就是查看返回给客户的内容所需要的全部内容。
-
@mjw:我的捕获块:
ex = ex.GetInnerMostException(); if (log.IsErrorEnabled) log.ErrorFormat("{0} failed : {1}", nameof(MakeVendorRequests), ex.Message); throw; -
@mjw:我的方法存根:
[WebMethod(EnableSession = true)] public async Task<string> MakeVendorRequests(ViperVendorViewModel vendorModel)
标签: javascript jquery asp.net ajax webforms