[英]Pass error message to ajax error handler
我使用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
拋出異常,我想在客戶端顯示錯誤消息。 但是,我似乎無法向用戶顯示一條好消息。
textStatus:
錯誤
errorThrown:
內部服務器錯誤
jqXHR.responseText:
{“Message”:“發生了一個或多個錯誤。”,“StackTrace”:“在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
處理程序。
這可能嗎?
要手動傳遞HttpStatusCodeResult
,請嘗試以下操作:
return new HttpStatusCodeResult(HttpStatusCode.BadRequest, e.Response.ReasonPhrase);
不幸的是,看起來你被鎖定在一個不會被處理為AJAX錯誤的返回類型中,所以你需要考慮更改控制器動作的返回類型以獲得所需的結果。 祝好運
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.