Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Display exception message from server on ajax error

Tags:

jquery

c#

ajax

I use the following code in order to display exception message from server on client:

    [WebMethod]
    [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
    public static void test(String text)
    {
               try
               {
                    throw new Exception("Hello");
               }

               catch (Exception ex)
               {
                HttpContext.Current.Response.Write(ex.Message);
                throw new Exception(ex.Message, ex.InnerException);
               }

    }

client-side:

    $.ajax({
        url: 'DownloadFile.aspx/test',
        type: 'POST',
        contentType: 'application/json; charset=utf-8',
        // Pass the value as JSON string                 
        // You might need to include json2.js for the JSON.stringify
        //method: 'http://www.json.org/json2.js',
        async: false,
        dataType: "json",
        data: '{ "text": "' + text'"}',
        success: function(Result) {

        },

        error: function(xhr, textStatus, errorThrown) {
            var err = eval("(" + xhr.responseText + ")");
            alert(err.Message);
        }
    });

when I use localhost, I get "Hello" in the alert popup. when I use the same code on remote server, I get general system error.

How can I get the exception message text displayed to the user?

like image 785
Inbal Avatar asked Nov 28 '12 16:11

Inbal


1 Answers

You need to set <customErrors mode="Off" /> in your web.config.

You can read more here

In general it looks like you have mode="RemoteOnly" which shows detailed exception messages only to the localhost.

like image 78
Igor Shakola Avatar answered Sep 21 '22 06:09

Igor Shakola