我在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