我想通过JQuery Post和Webmethod抛出一个处理异常,以便我可以将错误消息返回给警报函数。所以这只是一个测试脚本。C#Webmethod + Jquery发布错误消息
我有这个设置,并且正在捕获错误,现在我需要将服务器错误返回到警报函数!
[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";
这我帖子的脚本:
$("#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
什么问题? – Mairaj
尝试返回新的异常错误,而不是抛出新的异常错误 –