I am trying to throw a handled exception through JQuery Post and a Webmethod so that I can get an error message back to the alert function. So this is just a test script.
I have this set up, and am catching the error, now I need to return the server error back to the alert function!
[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";
And this my Post script:
$("#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
}
});
});
Thanks P