【发布时间】:2015-06-09 08:33:57
【问题描述】:
我正在尝试通过 JQuery Post 和 Web 方法抛出已处理的异常,以便我可以将错误消息返回给警报函数。所以这只是一个测试脚本。
我已经设置好了,并且正在捕获错误,现在我需要将服务器错误返回给警报功能!
[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public static string Test()
{
try
{
string value = null;
if (value.Length == 0) // <-- Causes exception
{
Console.WriteLine(value); // <-- Never reached
}
}
catch (Exception E)
{
StreamWriter sw = new StreamWriter(@"C:\inetpub\wwwroot\jQuery_AJAX_Database\error.txt", true);
sw.WriteLine(E.Message);
sw.Close();
sw.Dispose();
throw new Exception("error");
}
return "bla";
这是我的 Post 脚本:
$("#btnTest").click(function () {
$.ajax({
type: 'POST',
url: "my_test2.aspx/Test",
contentType: "application/json; charset=utf-8",
success: function (data) {
alert('Success!')
},
error: function (xhr, status, error) {
var exception = JSON.parse(xhr.responseText);
alert(exception.Message);
// Here I need to return server error
}
});
});
谢谢P
【问题讨论】:
-
尝试返回新的异常错误而不是抛出新的异常错误