我在jQuery中調用的aspx頁面上有一個WebMethod,我試圖讓它在彈出框中顯示拋出異常的消息,而不是在錯誤函數下運行代碼,調試器停止說「用戶未處理的異常」。我如何將錯誤返回給客戶端?jquery AJAX調用web方法不運行錯誤函數
[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
好的,我明白了。當你說返回一個錯誤,你的意思是返回一個字符串,其中的錯誤信息?我怎樣才能得到錯誤:function()來執行? – KateMak 2014-10-02 22:06:05
@KateMak返回新的HttpStatusCodeResult(errorCode,「Message」);如果你願意,errorCode可以是500。 – 2014-10-02 22:11:37
這沒有達到預期的效果... – KateMak 2014-10-02 22:15:11