Get Asp.Net Original Unhandled Exception Error Message in JQuery AJAX error Function in C#.Net VB.Net

In my previous articles I have explained how we can invoke or call server side method using JQuery AJAX. Here in this article I will explain “How we can handle ASP.Net WebMethod Unhandled Exception from JQuery AJAX error Function in C#.Net and VB.Net Web Applications“. It’s very important to catch or handle unhandled exceptions from JQuery AJAX function because using this function we can display error message properly to user, Log errors properly as well as it will help us a lot to pin point the error through the ASP.Net server error message and stacktrace. In many forums I found that developers more often confused whenever they did not get proper error message from JQuery error function. It’s for you. Try it. Hope it will resolve your debugging issues.

How to get Asp.Net Unhandled Exception in JQuery AJAX error Function in Csharp VB.Net

To prepare the Example use below HTML:

<asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="true"></asp:ScriptManager>
<tr><td>First Number:</td><td><asp:TextBox ID="TextBox1" runat="server"></asp:TextBox></td></tr>
<tr><td>Second Number:</td><td><asp:TextBox ID="TextBox2" runat="server"></asp:TextBox></td></tr>
<tr><td>SUM:</td><td><asp:Label ID="Label1" runat="server" Text="" Font-Bold="true"></asp:Label></td></tr>
<tr><td colspan="2"><asp:Button ID="Button1" runat="server" Text="Do SUM" /></td></tr>
<hr />
<div id="divStatus"></div>

Note: Add ScriptManager and set EnablePageMethods=”true”.

Prepare the Server Side sample WebMethod like below:
C# Code:

    public static Int32 GetSum(Int32 num1,Int32 num2)
        return num1 + num2;

VB.Net Code:

    <System.Web.Services.WebMethod()> _
    Public Shared Function GetSum(num1 As Int32, num2 As Int32) As Int32
        Return num1 + num2
    End Function

Catch Asp.Net WebMethod Internal Exception using JQuery:

    <script src="Scripts/jquery-1.4.1.min.js" type="text/javascript"></script>
    <script type="text/javascript">
        $(document).ready(function () {
            $('#<%=Button1.ClientID %>').click(function () {

                var num1 = $.trim($('#<%=TextBox1.ClientID %>').val());
                var num2 = $.trim($('#<%=TextBox2.ClientID %>').val());

                    type: "POST",
                    url: "Default.aspx/GetSum",
                    data: "{'num1':'" + num1 + "', 'num2':'" + num2 + "'}",
                    contentType: "application/json; charset=utf-8",
                    dataType: "json",
                    success: function (Response_msg) {
                        $('#<%=Label1.ClientID %>').text(Response_msg.d)

                    error: function (xhr) {
                        var r = jQuery.parseJSON(xhr.responseText);
                        var Server_Error_Message = r.Message;
                        var Server_Exception_Type = r.ExceptionType;
                        var Server_StackTrace = r.StackTrace;
                        $('#divStatus').html("<b>Asp.Net Error Message: </b>" + Server_Error_Message + "</br></br>" + "<b>ExceptionType: </b>" + Server_Exception_Type + "</br></br>" + "<b>Asp.Net StackTrace: </b>" + Server_StackTrace)
                return false;

Note: Observe the code “JQuery AJAX error: function” closely.

Hope this post will help you to identify server side error from JQuery AJAX function.

Download Code Example C#        Download Code Example VB.Net

Posted in .Net, Ajax,, C#, Client, JQuery, VB.Net

Leave a Reply

Your email address will not be published. Required fields are marked *