views:

1460

answers:

2

I'm using jQuery to handle all my ajax needs for an ASP.NET site coded in VB. When I use the built in $.ajax function to POST to a code-behind function and there is an exception, it simply exits the function, and shows an error on the client side.

Besides making debugging difficult when coding, the bigger issue is that the Application_Error function in Global.asax isn't firing when those exceptions occur. This is making it almost impossible to track production errors.

Why would it not bubble up to Application_Error? Is the answer to wrap the code-behind function in a Catch statement?

jQuery ajax function:

$.ajax({
 type: "POST",
 url: "page.aspx/GetData",
 data: "{'param':'" + param_value + "'}",
 contentType: "application/json; charset=utf-8",
 dataType: "json",

 success: function(msg) {
  processData(msg);
 },

 error: function(XMLHttpRequest, textStatus, errorThrown) {
  alert("Error: " + textStatus + " - " + errorThrown);
 }
});

VB.NET function:

<Script.Services.ScriptMethod()> _
<WebMethod()> _
Public Shared Function GetData(ByVal param As Integer) As String
 'Parse some data
 Return data
End Function
A: 

With .Net 3.5 you can pull in the ScriptManager object and register your javascript with it, this allows .Net to manage the scripts (it's not jsut for MS Ajax, it works with third party libraries), this could be a solution.

Martin
+1  A: 

I figured out a work around. Although I did not find a way to directly get the error to fire the Application_Error event, I discovered a property on the jQuery XMLHttpRequest object that contains the actual exception text.

Here is an example that will show the error in an alert:

error: function(XMLHttpRequest, textStatus, errorThrown) {
    alert("Error: " + XMLHttpRequest.responseText);
}

Another idea is to submit the error via AJAX to a web method that will handle all the error event logic.

Josiah I.